Last active
January 23, 2016 08:11
-
-
Save danieldk/1950a287108c34e7e74c to your computer and use it in GitHub Desktop.
golang-levenshtein 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 levenshtein | |
| import ( | |
| "math/rand" | |
| "testing" | |
| ) | |
| func randomString(r *rand.Rand, choice []rune, length int) []rune { | |
| str := make([]rune, 0, length) | |
| for i := 0; i < length; i++ { | |
| randRune := choice[rand.Intn(len(choice))] | |
| str = append(str, randRune) | |
| } | |
| return str | |
| } | |
| var alphabet = []rune{'a', 'b', 'c', 'd', 'e'} | |
| type benchPair struct { | |
| s1 []rune | |
| s2 []rune | |
| } | |
| func BenchmarkDistanceForStrings(b *testing.B) { | |
| r := rand.New(rand.NewSource(42)) | |
| pairs := make([]benchPair, b.N) | |
| for i := 0; i < b.N; i++ { | |
| s1 := randomString(r, alphabet, r.Intn(20)) | |
| s2 := randomString(r, alphabet, r.Intn(20)) | |
| pairs[i] = benchPair{s1, s2} | |
| } | |
| b.ResetTimer() | |
| for n := 0; n < b.N; n++ { | |
| pair := pairs[n] | |
| DistanceForStrings(pair.s1, pair.s2, DefaultLevenshtein) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment