-2

I have tested how golang performs on for loop

Just looping 50,000 times in python and golang and I found that it took 0.59 seconds in python while in golang it took 9.12 seconds

Can any experienced go developer tell me why Golang is too slow at for loop while it's a compiled language?


import (
    "fmt"
    "time"
)
func main()  {
    start := time.Now()

    for i := 0; i < 50000; i++ {
         fmt.Println("Index", i)
    }

    finish := time.Now().Sub(start).Seconds()
    fmt.Printf("Elapsed time was %.2f seconds.\n", finish)
}
  • Something is wrong with your golang installation if it's taking 9 seconds to loop 50k times. – rdas Jun 05 '20 at 02:18
  • 3
    You're not timing the "for loop" (which is nonsensical), you're timing how fast you can print to stdout (most likely a terminal). – JimB Jun 05 '20 at 02:26
  • It would be interesting to write the time to stderr while piping stdout to /dev/null – tdelaney Jun 05 '20 at 02:33
  • I ran this program and elapsed time was .12 seconds. – tdelaney Jun 05 '20 at 02:36
  • I found that it took 9.12 seconds in my windows machine which is 4 cores, but it took 0.59 seconds in Linux machine which is 8 cores – Mohamed Adel Jun 05 '20 at 02:39
  • I don't have Windows to test but I am surprised at the time. A Windows VM over a remote connection maybe?! – tdelaney Jun 05 '20 at 02:44
  • By the way, I think this is a valid test. It may say something about go terminal emulation on Windows because the code itself should be faster than python. – tdelaney Jun 05 '20 at 02:45
  • No, no remote connection. it's my local machine – Mohamed Adel Jun 05 '20 at 02:45
  • oh, I was running it from the vscode terminal, when I run it from the windows cmd it took 2.16 seconds, but it still a large number – Mohamed Adel Jun 05 '20 at 02:53

1 Answers1

9

You're not measuring what you think you're measuring, that's why you're getting "surprising" results.

You're timing how long it takes to format and print a string, rather than "how fast a for loop is".

Also, keep in mind that measuring how long it takes to print something depends not only on how the code gets compiled / interpreted, but also on where exactly you're printing: I/O performance depends on things that are beyond your program (maybe the OS, maybe some physical device etc).

Finally, if you tried to microbenchmark the performance of a loop that does absolutely nothing, a compiler could be able to detect that and simply optimize the loop out completely, leaving you with not measuring anything...

These micro-benchmarks in isolation are, most of the time, not useful. If you want to compare Python vs Go in terms of performance, it's usually a better idea to test on a real problem rather than something artificial. And then compare not only the raw performance, but also other characteristics of the code quality in general.

The bottom line is that there's too much wrong with this benchmark to get any useful conclusions.

Bruno Reis
  • 37,201
  • 11
  • 119
  • 156