On Sat, Apr 29, 2017 at 1:43 AM, T L <tapir....@gmail.com> wrote: > > package main > > import ( > "testing" > ) > > const N = 4096 > type T int64 > var a [N]T > > var globalSum T > > func sumByLoopArray_a(p *[N]T) T { > var sum T > for i := 0; i < len(p); i++ { > sum += T(p[i]) > } > return sum > } > > func sumByLoopArray_b(p *[N]T) T { > var sum T > for i := 0; i < len(*p); i++ { > sum += T((*p)[i]) > } > return sum > } > > //============================================ > > func Benchmark_LoopArray_0a(b *testing.B) { > for i := 0; i < b.N; i++ { > var sum T > for i := 0; i < len(a); i++ { > sum += T(a[i]) > } > globalSum = sum > } > } > > func Benchmark_LoopArray_0b(b *testing.B) { > for i := 0; i < b.N; i++ { > var sum T > p := &a > for i := 0; i < len(p); i++ { > sum += T(p[i]) > } > globalSum = sum > } > } > > func Benchmark_LoopArray_1a(b *testing.B) { > for i := 0; i < b.N; i++ { > globalSum = sumByLoopArray_a(&a) > } > } > > func Benchmark_LoopArray_1b(b *testing.B) { > for i := 0; i < b.N; i++ { > globalSum = sumByLoopArray_b(&a) > } > } > > /* output: > > $ go test . -bench=. > Benchmark_LoopArray_0a-4 300000 5248 ns/op > Benchmark_LoopArray_0b-4 300000 5240 ns/op > Benchmark_LoopArray_1a-4 500000 3942 ns/op > Benchmark_LoopArray_1b-4 300000 3936 ns/op > */ > > why?
Benchmarking is hard. If you really want to know why, you will have to look at the generated assembly code. There are likely to be differences there. For example, your 1a and 1b loops use a pointer to a global variable, but your 0a and 0b loops use a global variable directly. In general references through a pointer are more efficient than references to a named global variable. I don't know if that is the difference here, but it could be. It could also be something difficult to control for, like loop alignment. Ian -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.