Created
July 19, 2016 00:40
-
-
Save dustin/594e0dcae379b03fd184fb7272879b65 to your computer and use it in GitHub Desktop.
Testing interface overhead.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ go test -gcflags=-l -bench=. | |
testing: warning: no tests to run | |
PASS | |
BenchmarkDirect-4 500000000 3.36 ns/op | |
BenchmarkInterface-4 1000000000 3.46 ns/op | |
ok misc/intb 5.830s |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "testing" | |
type Int int64 | |
func (i *Int) Sum(i2 Int) { (*i) += i2 } | |
type Summer interface { | |
Sum(i Int) | |
} | |
func BenchmarkDirect(b *testing.B) { | |
var o Int | |
addVal := Int(18) | |
for i := 0; i < b.N; i++ { | |
o.Sum(addVal) | |
} | |
if int(o) != b.N*int(addVal) { | |
b.Errorf("got %v after %v iterations, wanted %v", o, b.N, b.N*int(addVal)) | |
} | |
} | |
func BenchmarkInterface(b *testing.B) { | |
var p Int | |
o := Summer(&p) | |
addVal := Int(18) | |
for i := 0; i < b.N; i++ { | |
o.Sum(addVal) | |
} | |
if int(p) != b.N*int(addVal) { | |
b.Errorf("got %v after %v iterations, wanted %v", o, b.N, b.N*int(addVal)) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ go test -bench=. | |
testing: warning: no tests to run | |
PASS | |
BenchmarkDirect-4 500000000 2.34 ns/op | |
BenchmarkInterface-4 500000000 3.85 ns/op | |
ok misc/intb 3.809s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment