Last active
July 5, 2017 06:55
-
-
Save wcharczuk/c61e247a4b6da560b8cb2b41934fc20f to your computer and use it in GitHub Desktop.
Benchmarks for common golang language features / stdlib items.
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 ( | |
| "sync" | |
| "testing" | |
| "time" | |
| ) | |
| func WithDefer(cleanup func()) { | |
| defer cleanup() | |
| } | |
| func RegularCall(cleanup func()) { | |
| cleanup() | |
| } | |
| func BenchmarkDefer(b *testing.B) { | |
| for n := 0; n < b.N; n++ { | |
| WithDefer(func() { | |
| time.Sleep(50 * time.Microsecond) | |
| }) | |
| } | |
| } | |
| func BenchmarkRegularCall(b *testing.B) { | |
| for n := 0; n < b.N; n++ { | |
| RegularCall(func() { | |
| time.Sleep(50 * time.Microsecond) | |
| }) | |
| } | |
| } | |
| func VarArgsCall(items ...string) int { | |
| time.Sleep(50 * time.Microsecond) | |
| return len(items) | |
| } | |
| func BenchmarkVarArgsUnpack(b *testing.B) { | |
| toUnpack := make([]string, 10) | |
| toUnpack[0] = "0" | |
| toUnpack[1] = "1" | |
| toUnpack[2] = "2" | |
| toUnpack[3] = "3" | |
| toUnpack[4] = "4" | |
| toUnpack[5] = "5" | |
| toUnpack[6] = "6" | |
| toUnpack[7] = "7" | |
| toUnpack[8] = "8" | |
| toUnpack[9] = "9" | |
| for n := 0; n < b.N; n++ { | |
| VarArgsCall(toUnpack...) | |
| } | |
| } | |
| func BenchmarkVarArgsDirect(b *testing.B) { | |
| for n := 0; n < b.N; n++ { | |
| VarArgsCall("0", "1", "3", "4", "5", "6", "7", "8", "9") | |
| } | |
| } | |
| func BenchmarkMutex(b *testing.B) { | |
| items := map[string]string{ | |
| "foo": "bar", | |
| } | |
| var lock sync.Mutex | |
| for n := 0; n < b.N; n++ { | |
| go func() { | |
| for n := 0; n < b.N; n++ { | |
| func() { | |
| lock.Lock() | |
| defer lock.Unlock() | |
| if items["foo"] == "bar" { | |
| items["foo"] = "buzz" | |
| } else if items["foo"] == "buzz" { | |
| items["foo"] = "bar" | |
| } | |
| }() | |
| func() { | |
| lock.Lock() | |
| defer lock.Unlock() | |
| if items["foo"] == "bar" { | |
| time.Sleep(50 * time.Millisecond) | |
| } | |
| }() | |
| } | |
| }() | |
| } | |
| } | |
| func BenchmarkRWMutex(b *testing.B) { | |
| items := map[string]string{ | |
| "foo": "bar", | |
| } | |
| var lock sync.RWMutex | |
| for n := 0; n < b.N; n++ { | |
| go func() { | |
| for n := 0; n < b.N; n++ { | |
| func() { | |
| lock.Lock() | |
| defer lock.Unlock() | |
| if items["foo"] == "bar" { | |
| items["foo"] = "buzz" | |
| } else if items["foo"] == "buzz" { | |
| items["foo"] = "bar" | |
| } | |
| }() | |
| func() { | |
| lock.RLock() | |
| defer lock.RUnlock() | |
| if items["foo"] == "bar" { | |
| time.Sleep(50 * time.Millisecond) | |
| } | |
| }() | |
| } | |
| }() | |
| } | |
| } | |
| func BenchmarkRange(b *testing.B) { | |
| for n := 0; n < b.N; n++ { | |
| var accum int | |
| items := make([]int, b.N) | |
| for index, item := range items { | |
| accum += index + item | |
| } | |
| } | |
| } | |
| func BenchmarkForIndex(b *testing.B) { | |
| for n := 0; n < b.N; n++ { | |
| var accum int | |
| items := make([]int, b.N) | |
| for index := 0; index < b.N; index++ { | |
| accum += index + items[index] | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment