Skip to content

Instantly share code, notes, and snippets.

@xthezealot
Last active July 7, 2017 17:24
Show Gist options
  • Save xthezealot/3b8968b63f1269fa4edbb1436b4fcdb8 to your computer and use it in GitHub Desktop.
Save xthezealot/3b8968b63f1269fa4edbb1436b4fcdb8 to your computer and use it in GitHub Desktop.
Go built-in benchs
var (
testString = "xkjXS98XarHFzP1DQu03fJTLQQ7raCvoSKwlAjtTUgxZIbRt9OoAQyByESBXFYnxoHetPiZdfLHUDSh6ix1MQ2hUWKli2c79RPCjJd8weljUMh7XjIToFjU7W67gYDwMAtihxI5twI33hL6Bran4wclJcOGZVmlkASofpsPSWmsR3PS8p01rrVxcCvuSXuyIQa0JMrLI"
testStringsSlice = make([]struct{}, 1000)
)
// 69.4 ns/op
func BenchmarkForString(b *testing.B) {
for i := 0; i < b.N; i++ {
for j := 0; j < len(testString); j++ {
if testString[j] == '0' {
}
}
}
}
// 168 ns/op
func BenchmarkForRangeString(b *testing.B) {
for i := 0; i < b.N; i++ {
for j := range testString {
if testString[j] == '0' {
}
}
}
}
// 280 ns/op
func BenchmarkForStringsSlice(b *testing.B) {
for i := 0; i < b.N; i++ {
for j := 0; j < len(testStringsSlice); j++ {
if testStringsSlice[j] == struct{}{} {
}
}
}
}
// 280 ns/op
func BenchmarkForRangeStringsSlice(b *testing.B) {
for i := 0; i < b.N; i++ {
for j := range testStringsSlice {
if testStringsSlice[j] == struct{}{} {
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment