Skip to content

Instantly share code, notes, and snippets.

@Egor3f
Created August 27, 2024 17:59
Show Gist options
  • Save Egor3f/bbf061a8467c9d71fdcf3d1a5d1d0f96 to your computer and use it in GitHub Desktop.
Save Egor3f/bbf061a8467c9d71fdcf3d1a5d1d0f96 to your computer and use it in GitHub Desktop.
package sparse_gogo
import (
"fmt"
"strconv"
"testing"
)
const MIN_SIZE = 4
const MAX_SIZE = 20
const STEP = 2
func createMap(size int) map[int]string {
m := make(map[int]string, size)
for i := 0; i < size; i++ {
m[i] = strconv.Itoa(i)
}
return m
}
func BenchmarkMapCreate(b *testing.B) {
for size := MIN_SIZE; size <= MAX_SIZE; size += STEP {
b.Run(fmt.Sprintf("Create map with size = %d", size), func(b *testing.B) {
for i := 0; i < b.N; i++ {
createMap(size)
}
})
}
}
func BenchmarkMapAccess(b *testing.B) {
for size := MIN_SIZE; size <= MAX_SIZE; size += STEP {
m := createMap(size)
for _, idx := range []int{0, size / 2, size - 1} {
b.Run(fmt.Sprintf("Access map size=%d, index=%d", size, 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 createSlice(size int) []entry {
slc := make([]entry, size)
for i := 0; i < size; i++ {
slc[i] = entry{i, strconv.Itoa(i)}
}
return slc
}
func BenchmarkSliceCreate(b *testing.B) {
for size := MIN_SIZE; size <= MAX_SIZE; size += STEP {
b.Run(fmt.Sprintf("Create slice size=%d", size), func(b *testing.B) {
createSlice(size)
})
}
}
func BenchmarkSliceAccess(b *testing.B) {
for size := MIN_SIZE; size <= MAX_SIZE; size += STEP {
slc := createSlice(size)
for _, idx := range []int{0, size / 2, size - 1} {
b.Run(fmt.Sprintf("Access slice size=%d, index %d", size, 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
break
}
}
_ = v
}
})
}
}
}
@Egor3f
Copy link
Author

Egor3f commented Aug 27, 2024

/opt/homebrew/Cellar/go/1.23.0/libexec/bin/go test -v ./... -bench . -run ^$
goos: darwin
goarch: arm64
pkg: sparse-gogo
cpu: Apple M1
BenchmarkMapCreate
BenchmarkMapCreate/Create_map_with_size_=_4
BenchmarkMapCreate/Create_map_with_size_=_4-8         	11289898	       105.7 ns/op
BenchmarkMapCreate/Create_map_with_size_=_6
BenchmarkMapCreate/Create_map_with_size_=_6-8         	 9512365	       126.5 ns/op
BenchmarkMapCreate/Create_map_with_size_=_8
BenchmarkMapCreate/Create_map_with_size_=_8-8         	 7979773	       150.7 ns/op
BenchmarkMapCreate/Create_map_with_size_=_10
BenchmarkMapCreate/Create_map_with_size_=_10-8        	 5151382	       233.9 ns/op
BenchmarkMapCreate/Create_map_with_size_=_12
BenchmarkMapCreate/Create_map_with_size_=_12-8        	 4339539	       276.4 ns/op
BenchmarkMapCreate/Create_map_with_size_=_14
BenchmarkMapCreate/Create_map_with_size_=_14-8        	 3479031	       344.6 ns/op
BenchmarkMapCreate/Create_map_with_size_=_16
BenchmarkMapCreate/Create_map_with_size_=_16-8        	 3068871	       388.5 ns/op
BenchmarkMapCreate/Create_map_with_size_=_18
BenchmarkMapCreate/Create_map_with_size_=_18-8        	 2768658	       432.3 ns/op
BenchmarkMapCreate/Create_map_with_size_=_20
BenchmarkMapCreate/Create_map_with_size_=_20-8        	 2504005	       479.7 ns/op
BenchmarkMapAccess
BenchmarkMapAccess/Access_map_size=4,_index=0
BenchmarkMapAccess/Access_map_size=4,_index=0-8       	536089028	         2.242 ns/op
BenchmarkMapAccess/Access_map_size=4,_index=2
BenchmarkMapAccess/Access_map_size=4,_index=2-8       	341886631	         3.513 ns/op
BenchmarkMapAccess/Access_map_size=4,_index=3
BenchmarkMapAccess/Access_map_size=4,_index=3-8       	311926306	         3.829 ns/op
BenchmarkMapAccess/Access_map_size=6,_index=0
BenchmarkMapAccess/Access_map_size=6,_index=0-8       	533844933	         2.244 ns/op
BenchmarkMapAccess/Access_map_size=6,_index=3
BenchmarkMapAccess/Access_map_size=6,_index=3-8       	313348413	         3.837 ns/op
BenchmarkMapAccess/Access_map_size=6,_index=5
BenchmarkMapAccess/Access_map_size=6,_index=5-8       	268463506	         4.470 ns/op
BenchmarkMapAccess/Access_map_size=8,_index=0
BenchmarkMapAccess/Access_map_size=8,_index=0-8       	531704348	         2.241 ns/op
BenchmarkMapAccess/Access_map_size=8,_index=4
BenchmarkMapAccess/Access_map_size=8,_index=4-8       	288948472	         4.155 ns/op
BenchmarkMapAccess/Access_map_size=8,_index=7
BenchmarkMapAccess/Access_map_size=8,_index=7-8       	235003166	         5.115 ns/op
BenchmarkMapAccess/Access_map_size=10,_index=0
BenchmarkMapAccess/Access_map_size=10,_index=0-8      	249727899	         4.812 ns/op
BenchmarkMapAccess/Access_map_size=10,_index=5
BenchmarkMapAccess/Access_map_size=10,_index=5-8      	250489911	         4.795 ns/op
BenchmarkMapAccess/Access_map_size=10,_index=9
BenchmarkMapAccess/Access_map_size=10,_index=9-8      	197720949	         6.089 ns/op
BenchmarkMapAccess/Access_map_size=12,_index=0
BenchmarkMapAccess/Access_map_size=12,_index=0-8      	248955961	         4.803 ns/op
BenchmarkMapAccess/Access_map_size=12,_index=6
BenchmarkMapAccess/Access_map_size=12,_index=6-8      	233981506	         5.107 ns/op
BenchmarkMapAccess/Access_map_size=12,_index=11
BenchmarkMapAccess/Access_map_size=12,_index=11-8     	197575040	         6.074 ns/op
BenchmarkMapAccess/Access_map_size=14,_index=0
BenchmarkMapAccess/Access_map_size=14,_index=0-8      	249710794	         4.815 ns/op
BenchmarkMapAccess/Access_map_size=14,_index=7
BenchmarkMapAccess/Access_map_size=14,_index=7-8      	249662456	         4.805 ns/op
BenchmarkMapAccess/Access_map_size=14,_index=13
BenchmarkMapAccess/Access_map_size=14,_index=13-8     	250638649	         4.795 ns/op
BenchmarkMapAccess/Access_map_size=16,_index=0
BenchmarkMapAccess/Access_map_size=16,_index=0-8      	249641529	         4.805 ns/op
BenchmarkMapAccess/Access_map_size=16,_index=8
BenchmarkMapAccess/Access_map_size=16,_index=8-8      	234195960	         5.107 ns/op
BenchmarkMapAccess/Access_map_size=16,_index=15
BenchmarkMapAccess/Access_map_size=16,_index=15-8     	234182630	         5.107 ns/op
BenchmarkMapAccess/Access_map_size=18,_index=0
BenchmarkMapAccess/Access_map_size=18,_index=0-8      	249830886	         4.803 ns/op
BenchmarkMapAccess/Access_map_size=18,_index=9
BenchmarkMapAccess/Access_map_size=18,_index=9-8      	250470021	         4.823 ns/op
BenchmarkMapAccess/Access_map_size=18,_index=17
BenchmarkMapAccess/Access_map_size=18,_index=17-8     	234764649	         5.115 ns/op
BenchmarkMapAccess/Access_map_size=20,_index=0
BenchmarkMapAccess/Access_map_size=20,_index=0-8      	249612622	         4.809 ns/op
BenchmarkMapAccess/Access_map_size=20,_index=10
BenchmarkMapAccess/Access_map_size=20,_index=10-8     	221008381	         5.428 ns/op
BenchmarkMapAccess/Access_map_size=20,_index=19
BenchmarkMapAccess/Access_map_size=20,_index=19-8     	208209422	         5.747 ns/op
BenchmarkSliceCreate
BenchmarkSliceCreate/Create_slice_size=4
BenchmarkSliceCreate/Create_slice_size=4-8            	1000000000	         0.0000005 ns/op
BenchmarkSliceCreate/Create_slice_size=6
BenchmarkSliceCreate/Create_slice_size=6-8            	1000000000	         0.0000007 ns/op
BenchmarkSliceCreate/Create_slice_size=8
BenchmarkSliceCreate/Create_slice_size=8-8            	1000000000	         0.0000017 ns/op
BenchmarkSliceCreate/Create_slice_size=10
BenchmarkSliceCreate/Create_slice_size=10-8           	1000000000	         0.0000008 ns/op
BenchmarkSliceCreate/Create_slice_size=12
BenchmarkSliceCreate/Create_slice_size=12-8           	1000000000	         0.0000004 ns/op
BenchmarkSliceCreate/Create_slice_size=14
BenchmarkSliceCreate/Create_slice_size=14-8           	1000000000	         0.0000007 ns/op
BenchmarkSliceCreate/Create_slice_size=16
BenchmarkSliceCreate/Create_slice_size=16-8           	1000000000	         0.0000011 ns/op
BenchmarkSliceCreate/Create_slice_size=18
BenchmarkSliceCreate/Create_slice_size=18-8           	1000000000	         0.0000004 ns/op
BenchmarkSliceCreate/Create_slice_size=20
BenchmarkSliceCreate/Create_slice_size=20-8           	1000000000	         0.0000006 ns/op
BenchmarkSliceAccess
BenchmarkSliceAccess/Access_slice_size=4,_index_0
BenchmarkSliceAccess/Access_slice_size=4,_index_0-8   	1000000000	         0.6799 ns/op
BenchmarkSliceAccess/Access_slice_size=4,_index_2
BenchmarkSliceAccess/Access_slice_size=4,_index_2-8   	626790325	         1.918 ns/op
BenchmarkSliceAccess/Access_slice_size=4,_index_3
BenchmarkSliceAccess/Access_slice_size=4,_index_3-8   	531321885	         2.236 ns/op
BenchmarkSliceAccess/Access_slice_size=6,_index_0
BenchmarkSliceAccess/Access_slice_size=6,_index_0-8   	1000000000	         0.6789 ns/op
BenchmarkSliceAccess/Access_slice_size=6,_index_3
BenchmarkSliceAccess/Access_slice_size=6,_index_3-8   	532471713	         2.234 ns/op
BenchmarkSliceAccess/Access_slice_size=6,_index_5
BenchmarkSliceAccess/Access_slice_size=6,_index_5-8   	415902913	         2.877 ns/op
BenchmarkSliceAccess/Access_slice_size=8,_index_0
BenchmarkSliceAccess/Access_slice_size=8,_index_0-8   	1000000000	         0.6781 ns/op
BenchmarkSliceAccess/Access_slice_size=8,_index_4
BenchmarkSliceAccess/Access_slice_size=8,_index_4-8   	469392894	         2.556 ns/op
BenchmarkSliceAccess/Access_slice_size=8,_index_7
BenchmarkSliceAccess/Access_slice_size=8,_index_7-8   	341765892	         3.515 ns/op
BenchmarkSliceAccess/Access_slice_size=10,_index_0
BenchmarkSliceAccess/Access_slice_size=10,_index_0-8  	1000000000	         0.6784 ns/op
BenchmarkSliceAccess/Access_slice_size=10,_index_5
BenchmarkSliceAccess/Access_slice_size=10,_index_5-8  	417678594	         2.877 ns/op
BenchmarkSliceAccess/Access_slice_size=10,_index_9
BenchmarkSliceAccess/Access_slice_size=10,_index_9-8  	289247582	         4.156 ns/op
BenchmarkSliceAccess/Access_slice_size=12,_index_0
BenchmarkSliceAccess/Access_slice_size=12,_index_0-8  	1000000000	         0.6784 ns/op
BenchmarkSliceAccess/Access_slice_size=12,_index_6
BenchmarkSliceAccess/Access_slice_size=12,_index_6-8  	375958694	         3.197 ns/op
BenchmarkSliceAccess/Access_slice_size=12,_index_11
BenchmarkSliceAccess/Access_slice_size=12,_index_11-8 	250492046	         4.796 ns/op
BenchmarkSliceAccess/Access_slice_size=14,_index_0
BenchmarkSliceAccess/Access_slice_size=14,_index_0-8  	1000000000	         0.6782 ns/op
BenchmarkSliceAccess/Access_slice_size=14,_index_7
BenchmarkSliceAccess/Access_slice_size=14,_index_7-8  	341826859	         3.509 ns/op
BenchmarkSliceAccess/Access_slice_size=14,_index_13
BenchmarkSliceAccess/Access_slice_size=14,_index_13-8 	221143905	         5.436 ns/op
BenchmarkSliceAccess/Access_slice_size=16,_index_0
BenchmarkSliceAccess/Access_slice_size=16,_index_0-8  	1000000000	         0.6783 ns/op
BenchmarkSliceAccess/Access_slice_size=16,_index_8
BenchmarkSliceAccess/Access_slice_size=16,_index_8-8  	313252165	         3.835 ns/op
BenchmarkSliceAccess/Access_slice_size=16,_index_15
BenchmarkSliceAccess/Access_slice_size=16,_index_15-8 	197847704	         6.072 ns/op
BenchmarkSliceAccess/Access_slice_size=18,_index_0
BenchmarkSliceAccess/Access_slice_size=18,_index_0-8  	1000000000	         0.6784 ns/op
BenchmarkSliceAccess/Access_slice_size=18,_index_9
BenchmarkSliceAccess/Access_slice_size=18,_index_9-8  	289117234	         4.154 ns/op
BenchmarkSliceAccess/Access_slice_size=18,_index_17
BenchmarkSliceAccess/Access_slice_size=18,_index_17-8 	176948239	         6.792 ns/op
BenchmarkSliceAccess/Access_slice_size=20,_index_0
BenchmarkSliceAccess/Access_slice_size=20,_index_0-8  	1000000000	         0.6786 ns/op
BenchmarkSliceAccess/Access_slice_size=20,_index_10
BenchmarkSliceAccess/Access_slice_size=20,_index_10-8 	267359920	         4.480 ns/op
BenchmarkSliceAccess/Access_slice_size=20,_index_19
BenchmarkSliceAccess/Access_slice_size=20,_index_19-8 	159685179	         7.500 ns/op
PASS
ok  	sparse-gogo	95.110s

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment