Sunday, May 19, 2024
HomeGolangFor loops Efficiency - Technical Dialogue

For loops Efficiency – Technical Dialogue


Hey, If you don’t thoughts I wished to know why Go “for” loops are sluggish, I attempted to do some easy “for” loops benchmarks, evaluating the efficiency of Go default compiler, TinyGo, and Graal Native Picture, and the outcome was so mush disappointing, though after I turned the GC off, TinyGo and Graal was so shut alternatively the default Go compiler was far-off, does the Go compiler do any optimizing for the loops in any respect?

BTW: TinyGo was higher even in calling C code (CGO).

Share particular benchmarks to get particular suggestions. Primarily based on what you’re describing, it appears more likely to me that go isn’t the software for the job you’re making an attempt to do. Check out Rust if low-level efficiency is of utmost significance however you need trendy tooling/ecosystem. I’m additionally fairly a fan of zig however it’s not prepared for primetime but.



1 Like

I’m not evaluating a low-level language to a low-level language, I’m evaluating a high-level to a high-level and making an attempt completely different compilers, don’t take it personally, thanks.

How was I taking it personally? Go is a software; one I exploit for sure duties the place it’s well-suited and don’t use others. I used to be questioning, based mostly on what you had been saying, whether or not it was the proper software so that you can be utilizing and instructed some options. I additionally instructed how you possibly can enhance your query to doubtlessly get extra helpful responses.

I’ll reiterate: put up particular benchmarks if you’d like an opportunity to get particular suggestions. Because it stands, your put up is obscure, consists of no code, and thus is more likely to produce obscure responses. That having been stated, there’s an opportunity you’re not even benchmarking what you suppose you’re benchmarking. Check out these hyperlinks:

https://www.reddit.com/r/golang/feedback/2p46ix/golang_simple_loop_slower_than_java/



2 Likes

Sorry about that, right here is an easy “for” loop benchmark in PureGo, TinyGo and Kotlin/Graal Native Picture (all of them and not using a GC):

Go and TinyGo Code :

Command used are :

$ GOGC=off go construct src/primary.go
$ GOGC=off tinygo construct -opt=2 src/primary.go

bundle primary
import (
    "fmt"
    "time"    
    )
func primary ( ) {
    var timeBefore = time.Now()
    doSomething()
    var timeAfter = time.Now()
    fmt.Println ( timeAfter.Sub(timeBefore) )

}

func doSomething () int {

    var x int = 0
    for i := 1 ; i <= 1000000 ; i++ {
        
        x += i 

    }
    return x
    
}

Kotlin Code :

$ native-image –gc=epsilon -march=native -O3 -jar primary.jar
// Compile a jar to a local executable and not using a GC

import kotlin.time.Length
import kotlin.time.measureTime

enjoyable primary () {

    var executionTime = measureTime { doSomething() }
    println ( executionTime )

 
}

enjoyable doSomething () :Int {

    var x:Int = 0
    for ( i in 1..1000000 ) {
        x += i
    }
    return x


}
Consequence : 
----------------------------- First Time | Second Time | Third Time----------------------
kotlin / Graal                1.816us      1.816us     1.537us
Go                            455.654µs    672.655µs   457.749µs
TinyGo                        5.448µs      5.308µs     3.562µs

Even when calling a C loop, TinyGo is quicker, and as you see right here, the loop doesn’t invoke any perform (like println).

Hey Dean, overlook what I stated, I attempted extra pure complicated “for” loops that may not be ignored (no shortcut for the compilers) to guarantee that the loops truly iterate, Graal Native Picture was so mush slower than PureGo and TinyGo, Tiny Go was a litte extra quicker :

TinyGo :              432.148669ms 443.742183ms 440.125731ms
Go :                  485.346782ms 487.894507ms 477.667357ms
GraalNM:              961.963131ms 974.069681ms 957.945913ms

TinyGo does some optimizations, however sadly It doesn’t assist all function of the newest Go model, Are there some flags that may be handed to the Go compiler for optimization?

Edit: After doing the identical take a look at in C and calling it from Go, TinyGo was rather a lot higher, I believe TinyGo can name C extra effectively :

Go :      445.494291ms 446.961961ms 442.149793ms
TinyGo :  85.634284ms  61.546723ms  88.512513ms

Artificial benchmarks are notoriously laborious to get appropriate, which is why folks are likely to desire real-world eventualities. I’m not tremendous acquainted with calling C from TinyGo however my understanding is that it does, certainly, have much less overhead:

https://aykevl.nl/2021/11/cgo-tinygo/

And from the TinyGo FAQ:

The usual Go compilers do just a few particular issues for CGo calls. That is crucial as a result of solely Go code can use the (small) Go stack whereas C code will want a a lot larger stack. A brand new compiler can keep away from this limitation if it ensures stacks are large enough for C, drastically decreasing the C ↔ Go calling overhead.

This appears in keeping with what you’re discovering. It appears like perhaps the reply is “use TinyGo”. What options of Go is it lacking that you just want? In language design, practically every thing is a collection of tradeoffs. Once more – I might query whether or not Go is making the proper tradeoffs (ease of use, easy concurrency with goroutines, compile velocity, rubbish assortment, succesful stdlib, tooling, ecosystem, and many others.) for the undertaking you are attempting to construct.



1 Like

Thanks, I made a decision to make use of it. As for What TinyGo is lacking, some options perhaps do no work as anticipated.

Sport engine. I hope Go & C are adequate for that.

Rust might be a greater software for that job. That stated, try:



1 Like

Thanks, Rust will not be good for psychological well being IMO.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments