Skip to content

Instantly share code, notes, and snippets.

@twsiyuan
Created June 14, 2021 17:00
Show Gist options
  • Save twsiyuan/ccbd3f2a8c91839d8b7e6b72f8a62cfb to your computer and use it in GitHub Desktop.
Save twsiyuan/ccbd3f2a8c91839d8b7e6b72f8a62cfb to your computer and use it in GitHub Desktop.
package main
import "testing"
func belongsToMap(imei string) bool {
list := map[string]bool{
"990008982968575": true,
"990008983020525": true,
"990008982964921": true,
"990008982968500": true,
"990008982967031": true,
"990008982966330": true,
"990008982966132": true,
"990008982966157": true,
"990008982966207": true,
"990008982966363": true,
}
if _, ok := list[imei]; ok {
return true
} else {
return false
}
}
func belongsToSwitch(imei string) bool {
switch imei {
case
"990008982968575",
"990008983020525",
"990008982964921",
"990008982968500",
"990008982967031",
"990008982966330",
"990008982966132",
"990008982966157",
"990008982966207",
"990008982966363":
return true
}
return false
}
func Benchmark1(b *testing.B) {
b.Run("Map", func(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = belongsToMap("test-imei")
}
})
b.Run("Switch", func(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = belongsToSwitch("test-imei")
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment