4 ms·
Re. Go. I've turned this code into a Go benchmark and run it with both the default cmd/compile (1.11) and gccgo (8.2.0). cmd/compile: BenchmarkFib-4
by ainar-g 8y ago
Re. Go.
I've turned this code into a Go benchmark and run it with both the default cmd/compile (1.11) and gccgo (8.2.0).
cmd/compile:
BenchmarkFib-4 1 16530409619 ns/op
PASS
ok command-line-arguments 16.533s
gccgo:
BenchmarkFib-4 2 776368644 ns/op
PASS
ok command-line-arguments 2.381s
So yeah, if you are doing heavy-weight math stuff in Go, you might consider switching to gccgo. You might get up to 20x performance boost.
Here is the gist, please tell me if I screwed up somewhere: https://gist.github.com/ainar-g/1bd363d41c441d9ebf05c0c0b9f2a529 https://gist.github.com/ainar-g/1bd363d41c441d9ebf05c0c0b9f2....
EDIT: After some disassembly and experimenting, it seems like what we see here is some clever unrolling and memoisation. If I change 46 from a constant to a variable, it becomes twice as slow. Plus there is this in disasm:
/tmp/go/fibbench_test.go:13
return fib(n-1) + fib(n-2)
30b9: e8 72 ff ff ff callq 3030 <command_line_arguments.fib>
30be: bf 25 00 00 00 mov $0x25,%edi
30c3: e8 68 ff ff ff callq 3030 <command_line_arguments.fib>
30c8: bf 24 00 00 00 mov $0x24,%edi
30cd: e8 5e ff ff ff callq 3030 <command_line_arguments.fib>
30d2: bf 23 00 00 00 mov $0x23,%edi
...
What puzzles me is why doesn't gcc do this for the C version. Even if I add an explicit __attribute__((const)).
- FabHK 8y agoSo you're saying it would run in less than 1/4 of the time of the Nim, C, C++ implementations? Sure it's running the same algo?
- ainar-g 8y agoI am as confused as you are. The C version is much slower on my PC. I've added the link to the code above. Please tell me if you see something fishy.
- ericpauley 8y agoMicrobenchmarks like this can be difficult to perform in practice, as gccgo can perform optimizations on pure functions that prevent Go's benchmarks from actually fully repeating a test. Also, this is a special case in which gccgo shines, specifically because it is completely cpu-bound. Generally, there isn't nearly as much of a performance difference.