Created
September 6, 2012 10:53
-
-
Save galaktor/3654745 to your computer and use it in GitHub Desktop.
func_bench
This file contains 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 functest | |
import "testing" | |
import "runtime" | |
type Target struct { | |
in chan TestFunc | |
state int | |
} | |
func (t *Target)DoSomething(param int) { | |
t.state *= param | |
} | |
type TestFunc func(t *Target) | |
func MyFunc(t *Target) { | |
t.DoSomething(42) | |
} | |
func BenchmarkDirectFunc(b *testing.B) { | |
t := Target{} | |
runtime.GOMAXPROCS(runtime.NumCPU()) | |
b.ResetTimer() | |
for i := 0; i < b.N; i++ { | |
t.DoSomething(42) | |
} | |
} | |
func BenchmarkChannelDontWait(b *testing.B) { | |
t := Target{make(chan TestFunc),0} | |
go func() { | |
for { | |
a := <-t.in | |
a(&t) | |
} | |
}() | |
runtime.GOMAXPROCS(runtime.NumCPU()) | |
b.ResetTimer() | |
for i := 0; i < b.N; i++ { | |
t.in <- MyFunc | |
} | |
} | |
func BenchmarkChannelWait(b *testing.B) { | |
res := make(chan bool) | |
t := Target{make(chan TestFunc),0} | |
go func() { | |
for { | |
a := <-t.in | |
a(&t) | |
res<- true | |
} | |
}() | |
runtime.GOMAXPROCS(runtime.NumCPU()) | |
b.ResetTimer() | |
for i := 0; i < b.N; i++ { | |
t.in <- MyFunc | |
<-res | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
on x86 WinXP, 4 core CPU:
P:\updates\test>go test -file func_test.go -bench=.*
testing: warning: no tests to run
PASS
BenchmarkDirectFunc 500000000 3.44 ns/op
testing: BenchmarkDirectFunc left GOMAXPROCS set to 4
BenchmarkChannelDontWait 200000 8593 ns/op
testing: BenchmarkChannelDontWait left GOMAXPROCS set to 4
BenchmarkChannelWait 200000 17031 ns/op
testing: BenchmarkChannelWait left GOMAXPROCS set to 4
ok /P/updates/test 7.485s