In Go, what is more efficient to return from a function: returning a uint
or returning a *uint
?
The function is called in the inner for-loop of a cpu-intensive library.
In Go, what is more efficient to return from a function: returning a uint
or returning a *uint
?
The function is called in the inner for-loop of a cpu-intensive library.
In general, whenever efficiency is the question, you should run benchmarks.
Let's create very simple examples:
func fuint() uint {
return 0
}
func fpuint() *uint {
var i uint
return &i
}
And the benchmarking code:
func BenchmarkUint(b *testing.B) {
for i := 0; i < b.N; i++ {
fuint()
}
}
func BenchmarkPuint(b *testing.B) {
for i := 0; i < b.N; i++ {
fpuint()
}
}
One thing to look out for: since we have very simple functions, code optimization might render the result useless (e.g. we might see the same result).
So instead we should test by disabling compiler optimizations. So let's test it like this:
go test -gcflags '-N -l' -bench . -benchmem
The test results:
BenchmarkUint-4 500000000 3.21 ns/op 0 B/op 0 allocs/op
BenchmarkPuint-4 100000000 22.4 ns/op 8 B/op 1 allocs/op
Just as we could have guessed: allocating an uint
value and returning a pointer to it is slower and requires an allocation (on the heap), while returning a simple uint
value does not.
But don't think this is always the case. During this test, we disabled compiler optimizations. When you compile your app to production, you will obviously not disable optimizations, so your real-life examples might not have such big difference, or no difference at all. Test / benchmark your actual code to see if returning a pointer actually makes any noticeable difference.
For reference, here is the result with compiler optimizations enabled (default behavior):
go test -bench . -benchmem
BenchmarkUint-4 2000000000 0.35 ns/op 0 B/op 0 allocs/op
BenchmarkPuint-4 2000000000 0.35 ns/op 0 B/op 0 allocs/op
We see no difference for fuint()
and fpuint()
at all in the specific examples above.