-
-
Save dmitshur/30a39a79e7db8c343911 to your computer and use it in GitHub Desktop.
Benchmark of two Go funcs template.
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 "fmt" | |
import "testing" | |
import "math/rand" | |
import "time" | |
var result bool | |
func StrCmp(n string) bool { | |
return n == "" | |
} | |
func LenCmp(n string) bool { | |
return len(n) == 0 | |
} | |
func BenchmarkStrCmp(b *testing.B) { | |
var r bool | |
str := "Sometimes Empty" | |
for i := 0; i < b.N; i++ { | |
r = StrCmp(str[0:randInt(0, len(str))]) | |
} | |
result = r | |
} | |
func BenchmarkLenCmp(b *testing.B) { | |
var r bool | |
str := "Sometimes Empty" | |
for i := 0; i < b.N; i++ { | |
r = LenCmp(str[0:randInt(0, len(str))]) | |
} | |
result = r | |
} | |
func randInt(min int, max int) int { | |
return min + rand.Intn(max-min) | |
} | |
func main() { | |
rand.Seed(time.Now().UnixNano()) | |
fmt.Println(testing.Benchmark(BenchmarkStrCmp)) | |
fmt.Println(testing.Benchmark(BenchmarkLenCmp)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Relevant: http://dave.cheney.net/2013/06/30/how-to-write-benchmarks-in-go