123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- package hashmap_test
- import (
- "fmt"
- "sync"
- "testing"
- "github.com/alphadose/haxmap"
- "github.com/cornelk/hashmap"
- "github.com/wecisecode/util/cmap"
- )
- func BenchmarkGoMap(t *testing.B) {
- // 1000000 loop
- // goos: darwin
- // goarch: amd64
- // pkg: test/hashmap
- // cpu: Intel(R) Core(TM) i7-4770HQ CPU @ 2.20GHz
- // BenchmarkGoMap
- // BenchmarkGoMap-8 1000000000 0.5068 ns/op 0 B/op 0 allocs/op
- // PASS
- // ok test/hashmap 14.880s
- // 100000 loop
- // goos: darwin
- // goarch: amd64
- // pkg: test/hashmap
- // cpu: Intel(R) Core(TM) i7-4770HQ CPU @ 2.20GHz
- // BenchmarkGoMap
- // BenchmarkGoMap-8 1000000000 0.02783 ns/op 0 B/op 0 allocs/op
- // PASS
- // ok test/hashmap 0.721s
- m := map[string]int{}
- t.StartTimer()
- for i := 0; i < 100000; i++ {
- s := fmt.Sprint(i)
- m[s] = i
- }
- t.StopTimer()
- }
- func BenchmarkGoSyncMap(t *testing.B) {
- // goos: darwin
- // goarch: amd64
- // pkg: test/hashmap
- // cpu: Intel(R) Core(TM) i7-4770HQ CPU @ 2.20GHz
- // BenchmarkGoSyncMap
- // BenchmarkGoSyncMap-8 1000000000 0.03480 ns/op 0 B/op 0 allocs/op
- // PASS
- // ok test/hashmap 2.544s
- mx := sync.Mutex{}
- m := map[string]int{}
- t.StartTimer()
- for i := 0; i < 100000; i++ {
- s := fmt.Sprint(i)
- mx.Lock()
- m[s] = i
- mx.Unlock()
- }
- t.StopTimer()
- }
- func BenchmarkSyncMap(t *testing.B) {
- // goos: darwin
- // goarch: amd64
- // pkg: test/hashmap
- // cpu: Intel(R) Core(TM) i7-4770HQ CPU @ 2.20GHz
- // BenchmarkSyncMap
- // BenchmarkSyncMap-8 1000000000 0.07106 ns/op 0 B/op 0 allocs/op
- // PASS
- // ok test/hashmap 1.068s
- m := sync.Map{}
- t.StartTimer()
- for i := 0; i < 100000; i++ {
- s := fmt.Sprint(i)
- m.Store(s, i)
- }
- t.StopTimer()
- }
- func BenchmarkCMap(t *testing.B) {
- // goos: darwin
- // goarch: amd64
- // pkg: test/hashmap
- // cpu: Intel(R) Core(TM) i7-4770HQ CPU @ 2.20GHz
- // BenchmarkCMap
- // BenchmarkCMap-8 1000000000 0.03648 ns/op 0 B/op 0 allocs/op
- // PASS
- // ok test/hashmap 0.769s
- m := cmap.New[string, any]()
- t.StartTimer()
- for i := 0; i < 100000; i++ {
- s := fmt.Sprint(i)
- m.Set(s, i)
- }
- t.StopTimer()
- }
- func BenchmarkHaxMap(t *testing.B) {
- // 1000000 loop
- // goos: darwin
- // goarch: amd64
- // pkg: test/hashmap
- // cpu: Intel(R) Core(TM) i7-4770HQ CPU @ 2.20GHz
- // BenchmarkHaxMap
- // BenchmarkHaxMap-8 1000000000 0.8453 ns/op 0 B/op 0 allocs/op
- // PASS
- // ok test/hashmap 65.175s
- // 100000 loop
- // goos: darwin
- // goarch: amd64
- // pkg: test/hashmap
- // cpu: Intel(R) Core(TM) i7-4770HQ CPU @ 2.20GHz
- // BenchmarkHaxMap
- // BenchmarkHaxMap-8 1000000000 0.05282 ns/op 0 B/op 0 allocs/op
- // PASS
- // ok test/hashmap 1.154s
- m := haxmap.New[string, int]()
- t.StartTimer()
- for i := 0; i < 100000; i++ {
- s := fmt.Sprint(i)
- m.Set(s, i)
- }
- t.StopTimer()
- }
- func BenchmarkHashMap(t *testing.B) {
- // 1000000 loop Crash
- // 100000 loop
- // goos: darwin
- // goarch: amd64
- // pkg: test/hashmap
- // cpu: Intel(R) Core(TM) i7-4770HQ CPU @ 2.20GHz
- // BenchmarkHashMap
- // BenchmarkHashMap-8 1 33365603372 ns/op 11402896 B/op 399822 allocs/op
- // PASS
- // ok test/hashmap 33.493s
- m := hashmap.New[string, int]()
- t.StartTimer()
- for i := 0; i < 100000; i++ {
- s := fmt.Sprint(i)
- m.Set(s, i)
- }
- t.StopTimer()
- }
|