BenchmarkOneLengthByte 2000000000 0.40 ns/op
BenchmarkTenLengthStringByte 50000000 35.0 ns/op
BenchmarkSixtyLengthByte 20000000 126 ns/op
BenchmarkOneLengthString 200000000 9.30 ns/op
BenchmarkTenLengthString 200000000 9.26 ns/op
BenchmarkSixtyLengthString 200000000 9.38 ns/op
BenchmarkOneLengthRegexp 10000000 294 ns/op
BenchmarkTenLengthRegexp 1000000 1083 ns/op
BenchmarkSixtyLengthRegexp 500000 4776 ns/op
Last active
March 21, 2018 21:43
-
-
Save smagch/9212149 to your computer and use it in GitHub Desktop.
golang: string equals length benchmark
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 ( | |
| "regexp" | |
| "testing" | |
| ) | |
| type str struct { | |
| Vals []string | |
| Regexps []*regexp.Regexp | |
| } | |
| func New() *str { | |
| vals := []string{ | |
| "f", | |
| "0123456789", | |
| "012345678901234567890123456789012345678901234567890123456789", | |
| } | |
| regexps := make([]*regexp.Regexp, 3) | |
| for i, v := range vals { | |
| r := regexp.MustCompile("^" + v + "$") | |
| regexps[i] = r | |
| } | |
| return &str{vals, regexps} | |
| } | |
| func equals(str, str2 string) bool { | |
| if len(str) != len(str2) { | |
| return false | |
| } | |
| for i, l := 0, len(str); i < l; i++ { | |
| if str[i] != str2[i] { | |
| return false | |
| } | |
| } | |
| return true | |
| } | |
| func BenchmarkOneLengthByte(b *testing.B) { | |
| var total int | |
| for i := 0; i < b.N; i++ { | |
| if 'f' == 'f' { | |
| total++ | |
| } | |
| } | |
| } | |
| func BenchmarkTenLengthStringByte(b *testing.B) { | |
| var total int | |
| str := New() | |
| for i := 0; i < b.N; i++ { | |
| if equals("0123456789", str.Vals[1]) { | |
| total++ | |
| } | |
| } | |
| } | |
| func BenchmarkSixtyLengthByte(b *testing.B) { | |
| var total int | |
| str := New() | |
| for i := 0; i < b.N; i++ { | |
| if equals("012345678901234567890123456789012345678901234567890123456789", str.Vals[2]) { | |
| total++ | |
| } | |
| } | |
| } | |
| func BenchmarkOneLengthString(b *testing.B) { | |
| var total int | |
| str := New() | |
| for i := 0; i < b.N; i++ { | |
| if "f" == str.Vals[0] { | |
| total++ | |
| } | |
| } | |
| } | |
| func BenchmarkTenLengthString(b *testing.B) { | |
| var total int | |
| str := New() | |
| for i := 0; i < b.N; i++ { | |
| if "0123456789" == str.Vals[1] { | |
| total++ | |
| } | |
| } | |
| } | |
| func BenchmarkSixtyLengthString(b *testing.B) { | |
| var total int | |
| str := New() | |
| for i := 0; i < b.N; i++ { | |
| if "012345678901234567890123456789012345678901234567890123456789" == str.Vals[2] { | |
| total++ | |
| } | |
| } | |
| } | |
| func BenchmarkOneLengthRegexp(b *testing.B) { | |
| var total int | |
| str := New() | |
| for i := 0; i < b.N; i++ { | |
| if str.Regexps[0].MatchString("f") { | |
| total++ | |
| } | |
| } | |
| } | |
| func BenchmarkTenLengthRegexp(b *testing.B) { | |
| var total int | |
| str := New() | |
| for i := 0; i < b.N; i++ { | |
| if str.Regexps[1].MatchString("0123456789") { | |
| total++ | |
| } | |
| } | |
| } | |
| func BenchmarkSixtyLengthRegexp(b *testing.B) { | |
| var total int | |
| str := New() | |
| for i := 0; i < b.N; i++ { | |
| if str.Regexps[2].MatchString("012345678901234567890123456789012345678901234567890123456789") { | |
| total++ | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment