Skip to content

Instantly share code, notes, and snippets.

@dmitshur
Forked from dinedal/gist:a4a95a871846d4e68937
Last active December 19, 2015 02:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmitshur/30a39a79e7db8c343911 to your computer and use it in GitHub Desktop.
Save dmitshur/30a39a79e7db8c343911 to your computer and use it in GitHub Desktop.
Benchmark of two Go funcs template.
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))
}
@dmitshur
Copy link
Author

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