Created
August 27, 2024 16:24
-
-
Save Egor3f/1f006ac59b6471f6ff01752a59cc335f to your computer and use it in GitHub Desktop.
This file contains 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 sparse_gogo | |
import ( | |
"fmt" | |
"testing" | |
) | |
func BenchmarkMapCreate(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
m := map[int]string{ | |
5: "lol", | |
5413: "kek", | |
17842346: "cheburek", | |
} | |
_ = m | |
} | |
} | |
func BenchmarkMapAccess(b *testing.B) { | |
m := map[int]string{ | |
5: "lol", | |
5413: "kek", | |
17842346: "cheburek", | |
} | |
for _, idx := range []int{5, 5413, 17842346, 12345} { | |
b.Run(fmt.Sprintf("Access index %d", idx), func(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
v, ok := m[idx] | |
_, _ = v, ok | |
} | |
}) | |
} | |
} | |
type entry struct { | |
idx int | |
val string | |
} | |
func BenchmarkSliceCreate(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
m := []entry{ | |
{5, "lol"}, | |
{5413, "kek"}, | |
{17842346, "cheburek"}, | |
} | |
_ = m | |
} | |
} | |
func BenchmarkSliceAccess(b *testing.B) { | |
slc := []entry{ | |
{5, "lol"}, | |
{5413, "kek"}, | |
{17842346, "cheburek"}, | |
} | |
for _, idx := range []int{5, 5413, 17842346, 12345} { | |
b.Run(fmt.Sprintf("Access index %d", idx), func(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
var v string | |
for _, ent := range slc { | |
if ent.idx == idx { | |
v = ent.val | |
} | |
} | |
_ = v | |
} | |
}) | |
} | |
} |
Author
Egor3f
commented
Aug 27, 2024
func BenchmarkSwitchAccess(b *testing.B) {
get := func(idx int) (string, bool) {
switch idx {
case 5:
return "lol", true
case 5413:
return "kek", true
case 17842346:
return "cheburek", true
}
return "", false
}
for _, idx := range []int{5, 5413, 17842346, 12345} {
b.Run(fmt.Sprintf("Access index %d", idx), func(b *testing.B) {
for i := 0; i < b.N; i++ {
v, ok := get(idx)
_, _ = v, ok
}
})
}
}
goos: linux
goarch: amd64
cpu: 13th Gen Intel(R) Core(TM) i7-13700
BenchmarkMapCreate-24 55882170 21.43 ns/op
BenchmarkMapAccess/Access_index_5-24 762712117 1.572 ns/op
BenchmarkMapAccess/Access_index_5413-24 671014778 1.756 ns/op
BenchmarkMapAccess/Access_index_17842346-24 606093943 1.941 ns/op
BenchmarkMapAccess/Access_index_12345-24 343195396 4.275 ns/op
BenchmarkSliceCreate-24 1000000000 0.1173 ns/op
BenchmarkSliceAccess/Access_index_5-24 1000000000 0.4934 ns/op
BenchmarkSliceAccess/Access_index_5413-24 1000000000 0.4927 ns/op
BenchmarkSliceAccess/Access_index_17842346-24 1000000000 0.5098 ns/op
BenchmarkSliceAccess/Access_index_12345-24 1000000000 0.5216 ns/op
BenchmarkSwitchAccess/Access_index_5-24 1000000000 0.1077 ns/op
BenchmarkSwitchAccess/Access_index_5413-24 1000000000 0.1211 ns/op
BenchmarkSwitchAccess/Access_index_17842346-24 1000000000 0.1049 ns/op
BenchmarkSwitchAccess/Access_index_12345-24 1000000000 0.1027 ns/op
PASS
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment