Skip to content

Instantly share code, notes, and snippets.

@divan
Created November 12, 2016 19:11
Show Gist options
  • Save divan/c3500bfbe0801b4dd10b8d596837cde7 to your computer and use it in GitHub Desktop.
Save divan/c3500bfbe0801b4dd10b8d596837cde7 to your computer and use it in GitHub Desktop.
package funcptr
import "testing"
type LargeStruct struct {
f1, f2, f3, f4, f5, f6, f7, f8, f9, f10 uint64
a1, a2, a3, a4, a5, a6, a7, a8, a9, a10 uint64
b1, b2, b3, b4, b5, b6, b7, b8, b9, b10 uint64
c1, c2, c3, c4, c5, c6, c7, c8, c9, c10 uint64
d1, d2, d3, d4, d5, d6, d7, d8, d9, d10 uint64
e1, e2, e3, e4, e5, e6, e7, e8, e9, e10 uint64
j1, j2, j3, j4, j5, j6, j7, j8, j9, j10 uint64
k1, k2, k3, k4, k5, k6, k7, k8, k9, k10 uint64
l1, l2, l3, l4, l5, l6, l7, l8, l9, l10 uint64
p1, p2, p3, p4, p5, p6, p7, p8, p9, p10 uint64
}
func Foo(arr [2e6]int64) int { return len(arr) }
func FooPtr(arr *[2e6]int64) int { return len(arr) }
func FooArray(s LargeStruct) int { return int(s.f1 + s.f2) }
func FooArrayPtr(s *LargeStruct) int { return int(s.f1 + s.f2) }
func BenchmarkFooArray(b *testing.B) {
a := [2e6]int64{}
for i := 0; i < b.N; i++ {
Foo(a)
}
}
func BenchmarkFooArrayPtr(b *testing.B) {
a := [2e6]int64{}
for i := 0; i < b.N; i++ {
FooPtr(&a)
}
}
func BenchmarkFoo(b *testing.B) {
a := LargeStruct{}
for i := 0; i < b.N; i++ {
FooArray(a)
}
}
func BenchmarkFooPtr(b *testing.B) {
a := LargeStruct{}
for i := 0; i < b.N; i++ {
FooArrayPtr(&a)
}
}
@divan
Copy link
Author

divan commented Nov 12, 2016

go test -bench . -benchmem .
testing: warning: no tests to run
BenchmarkFooArray-4      	     300	   4822227 ns/op	16060526 B/op	       1 allocs/op
BenchmarkFooArrayPtr-4   	2000000000	         0.35 ns/op	       0 B/op	       0 allocs/op
BenchmarkFoo-4           	50000000	        26.0 ns/op	       0 B/op	       0 allocs/op
BenchmarkFooPtr-4        	2000000000	         0.34 ns/op	       0 B/op	       0 allocs/op
PASS
ok  	test/funcptrs	4.750s

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment