Created
December 7, 2017 02:20
-
-
Save usk81/219e1d4b04c068562b0859e441f2c1c0 to your computer and use it in GitHub Desktop.
slice string filtering
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 utils | |
import ( | |
"testing" | |
) | |
func BenchmarkFilterSlice(b *testing.B) { | |
p := []string{"pika", "pi", "chu", "piika"} | |
p2 := []string{"pika", "pi", "chu"} | |
for i := 0; i < b.N; i++ { | |
tb := []string{} | |
for _, bi := range p { | |
for _, b := range p2 { | |
if bi == b { | |
tb = append(tb, bi) | |
break | |
} | |
} | |
} | |
p = tb | |
} | |
} | |
func BenchmarkFilterSlice2(b *testing.B) { | |
p := []string{"pika", "pi", "chu", "piika"} | |
p2 := []string{"pika", "pi", "chu"} | |
for i := 0; i < b.N; i++ { | |
tb := make([]string, len(p)) | |
var cnt int | |
for _, bi := range p { | |
for _, b := range p2 { | |
if bi == b { | |
tb[cnt] = bi | |
cnt++ | |
break | |
} | |
} | |
} | |
p = tb[:cnt] | |
} | |
} | |
// BenchmarkFilterSlice-8 5000000 244 ns/op 112 B/op 3 allocs/op | |
// BenchmarkFilterSlice2-8 20000000 94.0 ns/op 48 B/op 1 allocs/op |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment