Skip to content

Instantly share code, notes, and snippets.

@smagch
Last active February 18, 2019 08:17
Show Gist options
  • Select an option

  • Save smagch/9272790 to your computer and use it in GitHub Desktop.

Select an option

Save smagch/9272790 to your computer and use it in GitHub Desktop.
Golang: map vs struct loopkup benchmark
BenchmarkStruct1	50000000	        41.1 ns/op
BenchmarkMap1	50000000	        43.5 ns/op
BenchmarkStruct2	50000000	        42.8 ns/op
BenchmarkMap2	50000000	        44.4 ns/op
BenchmarkStruct3	50000000	        45.3 ns/op
BenchmarkMap3	50000000	        37.0 ns/op
BenchmarkStruct4	50000000	        42.2 ns/op
BenchmarkMap4	50000000	        37.3 ns/op
package main
import "testing"
type Methods struct {
Handle string
Get string
Post string
Delete string
Put string
Patch string
Options string
}
func New() *Methods {
return &Methods{
"handle string",
"get string",
"post string",
"delete string",
"put string",
"patch string",
"options string",
}
}
func getCase1() []string {
return []string{"GET", "POST", "MOVE", "DELETE", "PATCH", "HEAD", "OPTIONS", "COPY"}
}
func getCase2() []string {
// no "PUT"
return []string{"GET", "PUT", "MOVE", "OPTIONS", "COPY"}
}
func getCase3() []string {
return []string{"PUT", "PATCH", "OPTIONS"}
}
func getCase4() []string {
return []string{"DELETE", "OPTIONS"}
}
func PassString(str string) {
}
func GetMethodMap() map[string]string {
return map[string]string {
"HANDLE": "handle string",
"GET": "get string",
"POST": "post string",
"DELETE": "delete string",
"PUT": "put string",
"PATCH": "patch string",
"OPTIONS": "options string",
}
}
func BenchmarkStruct1(b *testing.B) {
methods := getCase1()
methodMap := New()
l := len(methods)
for i := 0; i < b.N; i++ {
var target string
switch methods[i % l] {
case "GET", "HEAD":
target = methodMap.Get
case "POST":
target = methodMap.Post
case "PUT":
target = methodMap.Put
case "DELETE":
target = methodMap.Delete
case "PATCH":
target = methodMap.Patch
case "OPTIONS":
target = methodMap.Options
default:
target = methodMap.Handle
}
PassString(target)
}
}
func BenchmarkMap1(b *testing.B) {
methods := getCase1()
methodMap := GetMethodMap()
l := len(methods)
for i := 0; i < b.N; i++ {
m := methods[i % l]
target, ok := methodMap[m]
if !ok {
target, _ = methodMap["HANDLE"]
}
PassString(target)
}
}
func BenchmarkStruct2(b *testing.B) {
methods := getCase2()
methodMap := New()
l := len(methods)
for i := 0; i < b.N; i++ {
var target string
switch methods[i % l] {
case "GET", "HEAD":
target = methodMap.Get
case "POST":
target = methodMap.Post
case "PUT":
target = methodMap.Put
case "DELETE":
target = methodMap.Delete
case "PATCH":
target = methodMap.Patch
case "OPTIONS":
target = methodMap.Options
default:
target = methodMap.Handle
}
PassString(target)
}
}
func BenchmarkMap2(b *testing.B) {
methods := getCase2()
methodMap := GetMethodMap()
l := len(methods)
for i := 0; i < b.N; i++ {
m := methods[i % l]
target, ok := methodMap[m]
if !ok {
target, _ = methodMap["HANDLE"]
}
PassString(target)
}
}
func BenchmarkStruct3(b *testing.B) {
methods := getCase3()
methodMap := New()
l := len(methods)
for i := 0; i < b.N; i++ {
var target string
switch methods[i % l] {
case "GET", "HEAD":
target = methodMap.Get
case "POST":
target = methodMap.Post
case "PUT":
target = methodMap.Put
case "DELETE":
target = methodMap.Delete
case "PATCH":
target = methodMap.Patch
case "OPTIONS":
target = methodMap.Options
default:
target = methodMap.Handle
}
PassString(target)
}
}
func BenchmarkMap3(b *testing.B) {
methods := getCase3()
methodMap := GetMethodMap()
l := len(methods)
for i := 0; i < b.N; i++ {
m := methods[i % l]
target, ok := methodMap[m]
if !ok {
target, _ = methodMap["HANDLE"]
}
PassString(target)
}
}
func BenchmarkStruct4(b *testing.B) {
methods := getCase4()
methodMap := New()
l := len(methods)
for i := 0; i < b.N; i++ {
var target string
switch methods[i % l] {
case "GET", "HEAD":
target = methodMap.Get
case "POST":
target = methodMap.Post
case "PUT":
target = methodMap.Put
case "DELETE":
target = methodMap.Delete
case "PATCH":
target = methodMap.Patch
case "OPTIONS":
target = methodMap.Options
default:
target = methodMap.Handle
}
PassString(target)
}
}
func BenchmarkMap4(b *testing.B) {
methods := getCase4()
methodMap := GetMethodMap()
l := len(methods)
for i := 0; i < b.N; i++ {
m := methods[i % l]
target, ok := methodMap[m]
if !ok {
target, _ = methodMap["HANDLE"]
}
PassString(target)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment