Skip to content

Instantly share code, notes, and snippets.

@dinedal
Created June 27, 2013 22:38
Show Gist options
  • Save dinedal/a4a95a871846d4e68937 to your computer and use it in GitHub Desktop.
Save dinedal/a4a95a871846d4e68937 to your computer and use it in GitHub Desktop.
package main
import "fmt"
import "testing"
import "math/rand"
import "time"
func StrCmp(n string) bool {
return n == ""
}
func LenCmp(n string) bool {
return len(n) == 0
}
func BenchmarkStrCmp(b *testing.B) {
str := "Sometimes Empty"
for i := 0; i < b.N; i++ {
_ = StrCmp(str[0:randInt(0, len(str))])
}
}
func BenchmarkLenCmp(b *testing.B) {
str := "Sometimes Empty"
for i := 0; i < b.N; i++ {
_ = LenCmp(str[0:randInt(0, len(str))])
}
}
func randInt(min int, max int) int {
return min + rand.Intn(max-min)
}
func main() {
rand.Seed(time.Now().UTC().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