github.com/egonelbre/exp@v0.0.0-20240430123955-ed1d3aa93911/bench/imaps/bench_test.go (about)

     1  package imaps
     2  
     3  import (
     4  	"strconv"
     5  	"testing"
     6  )
     7  
     8  type Alpha struct {
     9  	Name  string
    10  	Value int
    11  	X     int32
    12  	Y     int16
    13  	Z     int64
    14  }
    15  
    16  type Beta struct {
    17  	Name  string
    18  	Value int
    19  	X     int32
    20  	Y     int16
    21  	Z     int64
    22  }
    23  
    24  type token struct{}
    25  
    26  func BenchmarkAlpha(b *testing.B) {
    27  	temp := map[interface{}]token{}
    28  	temp[1] = token{}
    29  	temp[keyAlpha(-1)] = token{}
    30  	for i := 0; i < b.N; i++ {
    31  		_ = temp[keyAlpha(i)]
    32  	}
    33  }
    34  
    35  func BenchmarkBeta(b *testing.B) {
    36  	temp := map[interface{}]token{}
    37  	temp[1] = token{}
    38  	temp[keyBeta(-1)] = token{}
    39  	for i := 0; i < b.N; i++ {
    40  		_ = temp[keyBeta(i)]
    41  	}
    42  }
    43  func BenchmarkInt(b *testing.B) {
    44  	temp := map[interface{}]token{}
    45  	temp[1] = token{}
    46  	temp[keyBeta(-1)] = token{}
    47  	for i := 0; i < b.N; i++ {
    48  		_ = temp[strconv.Itoa(i)]
    49  	}
    50  }
    51  
    52  func BenchmarkBetaDirect(b *testing.B) {
    53  	temp := map[Beta]token{}
    54  	temp[Beta{}] = token{}
    55  	for i := 0; i < b.N; i++ {
    56  		_ = temp[Beta{Name: "x", Value: i}]
    57  	}
    58  }
    59  
    60  //go:noinline
    61  func keyAlpha(v int) interface{} {
    62  	return Alpha{Name: "x", Value: v}
    63  }
    64  
    65  //go:noinline
    66  func keyBeta(v int) interface{} {
    67  	return Beta{Name: "x", Value: v}
    68  }