Skip to content

Instantly share code, notes, and snippets.

@seanlinsley
Last active January 3, 2023 17:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seanlinsley/dd7b2bf8d09b6ba710d27044794f86c5 to your computer and use it in GitHub Desktop.
Save seanlinsley/dd7b2bf8d09b6ba710d27044794f86c5 to your computer and use it in GitHub Desktop.
// sourced from https://github.com/golang/go/issues/13271, with additions at the bottom of the file
package main
import (
"testing"
//
"reflect"
"unsafe"
)
func BenchmarkMap1S(b *testing.B) {
m := make(map[string]int)
k_tmp := [12]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1}
k := string(k_tmp[:])
m[k] = 1
b.ResetTimer()
counter := 0
for i := 0; i < b.N; i++ {
counter += m[k]
}
}
func BenchmarkMap1S3U(b *testing.B) {
m := make(map[string]int)
k_tmp := [3]uint32{0, 1, 2}
h := reflect.StringHeader{Data: uintptr(unsafe.Pointer(&k_tmp[0])), Len: len(k_tmp) * int(unsafe.Sizeof(k_tmp[0]))}
k := *(*string)(unsafe.Pointer(&h))
m[k] = 1
b.ResetTimer()
counter := 0
for i := 0; i < b.N; i++ {
hh := reflect.StringHeader{Data: uintptr(unsafe.Pointer(&k_tmp[0])), Len: 12}
kk := *(*string)(unsafe.Pointer(&hh))
counter += m[kk]
}
}
func BenchmarkMap1Str3U(b *testing.B) {
m := make(map[string]int)
k_tmp := [3]uint32{0, 1, 2}
h := (*[12]byte)(unsafe.Pointer(&k_tmp[0]))
k := string(h[:])
m[k] = 1
b.ResetTimer()
counter := 0
for i := 0; i < b.N; i++ {
hh := (*[12]byte)(unsafe.Pointer(&k_tmp[0]))
kk := string(hh[:])
counter += m[kk]
}
}
func BenchmarkMap12C(b *testing.B) {
m := make(map[[12]byte]int)
k := [12]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1}
m[k] = 1
b.ResetTimer()
counter := 0
for i := 0; i < b.N; i++ {
counter += m[k]
}
}
func BenchmarkMap3U(b *testing.B) {
m := make(map[[3]uint32]int)
k := [3]uint32{0, 1, 2}
m[k] = 1
b.ResetTimer()
counter := 0
for i := 0; i < b.N; i++ {
counter += m[k]
}
}
func BenchmarkMap2L(b *testing.B) {
m := make(map[[2]uint64]int)
k := [2]uint64{1234, 456}
m[k] = 1
b.ResetTimer()
counter := 0
for i := 0; i < b.N; i++ {
counter += m[k]
}
}
func BenchmarkMap1L(b *testing.B) {
m := make(map[uint64]int)
k := uint64(1234)
m[k] = 1
b.ResetTimer()
counter := 0
for i := 0; i < b.N; i++ {
counter += m[k]
}
}
func BenchmarkMap1K(b *testing.B) {
m := make(map[interface{}]int)
k := &struct {
k interface{}
}{k: 1234}
m[k] = 1
b.ResetTimer()
counter := 0
for i := 0; i < b.N; i++ {
counter += m[k]
}
}
type M struct {
k uint64
}
func BenchmarkMap1M(b *testing.B) {
m := make(map[M]int)
k := M{1234}
m[k] = 1
b.ResetTimer()
counter := 0
for i := 0; i < b.N; i++ {
counter += m[k]
}
}
func main() {
}
type Oid uint64
type PostgresStatementKey struct {
DatabaseOid Oid // OID of database in which the statement was executed
UserOid Oid // OID of user who executed the statement
QueryID int64 // Postgres 9.4+: Internal hash code, computed from the statement's parse tree
}
func BenchmarkPGSK1(b *testing.B) {
m := make(map[PostgresStatementKey]int)
k := PostgresStatementKey{ DatabaseOid: 1, UserOid: 2, QueryID: 3 }
m[k] = 1
b.ResetTimer()
counter := 0
for i := 0; i < b.N; i++ {
counter += m[k]
}
}
func BenchmarkPGSK2(b *testing.B) {
m := make([]map[int64]int, 100)
database_idx := 0
k := int64(3) // QueryID
if m[database_idx] == nil {
m[database_idx] = make(map[int64]int)
}
m[database_idx][k] = 1
b.ResetTimer()
counter := 0
for i := 0; i < b.N; i++ {
counter += m[database_idx][k]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment