Been picking up Go recently and I'm a big fan, coming from a Java background.
I was comparing the languages in different ways, and was surprised that a simple loop counting up to 20 billion took substantially longer in Golang vs Java.
Was wondering if anyone can give any insight for if I'm missing something here. This is what I did:
Java
Wrote the below code, executed it from a normal main()
method, built an executable jar with Gradle, and executed it from the command line with the command: java -jar build/libs/my-executable.jar
private void countToTwentyBillion() {
long count = 0;
long start = System.currentTimeMillis();
for (int k = 0; k < 10; k++) {
System.out.println("On step " + k);
for (int i = 0; i < 2_000_000_000; i++) {
// Do nothing but count
count++;
}
}
long end = System.currentTimeMillis();
System.out.println("Total time took: " + (end - start) + " ms to get at count: " + count);
}
Across 3 separate trials, I got these results:
// Total time took: 396 ms to get at count: 20000000000
// Total time took: 393 ms to get at count: 20000000000
// Total time took: 388 ms to get at count: 20000000000
// 392 ms average
Go
Built this file in Go, built it with 'go build' and executed at the command line with ./loop-counter
package main
import (
"fmt"
"time"
)
func main() {
count := 0
nanos := time.Now().UnixNano()
start := nanos / 1000000
for i := 0; i < 10; i++ {
fmt.Printf("On step %d
", i)
for k := 0; k < 2000000000; k++ {
count++
}
}
nanos = time.Now().UnixNano()
end := nanos / 1000000
timeLength := end - start
fmt.Printf("Total time took: %d ms to get at count: %d
", timeLength, count)
}
After 3 separate trials, I got these results:
// Total time took: 5812 ms to get at count: 20000000000
// Total time took: 5834 ms to get at count: 20000000000
// Total time took: 5907 ms to get at count: 20000000000
// 5,851 ms average
I went into this expecting Go to be faster and ended up being surprised. All trials were done on the same machine with the same conditions.
Can anyone tell what gives?
Thanks