hashmap_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package hashmap_test
  2. import (
  3. "fmt"
  4. "sync"
  5. "testing"
  6. "github.com/alphadose/haxmap"
  7. "github.com/cornelk/hashmap"
  8. "github.com/wecisecode/util/cmap"
  9. )
  10. func BenchmarkGoMap(t *testing.B) {
  11. // 1000000 loop
  12. // goos: darwin
  13. // goarch: amd64
  14. // pkg: test/hashmap
  15. // cpu: Intel(R) Core(TM) i7-4770HQ CPU @ 2.20GHz
  16. // BenchmarkGoMap
  17. // BenchmarkGoMap-8 1000000000 0.5068 ns/op 0 B/op 0 allocs/op
  18. // PASS
  19. // ok test/hashmap 14.880s
  20. // 100000 loop
  21. // goos: darwin
  22. // goarch: amd64
  23. // pkg: test/hashmap
  24. // cpu: Intel(R) Core(TM) i7-4770HQ CPU @ 2.20GHz
  25. // BenchmarkGoMap
  26. // BenchmarkGoMap-8 1000000000 0.02783 ns/op 0 B/op 0 allocs/op
  27. // PASS
  28. // ok test/hashmap 0.721s
  29. m := map[string]int{}
  30. t.StartTimer()
  31. for i := 0; i < 100000; i++ {
  32. s := fmt.Sprint(i)
  33. m[s] = i
  34. }
  35. t.StopTimer()
  36. }
  37. func BenchmarkGoSyncMap(t *testing.B) {
  38. // goos: darwin
  39. // goarch: amd64
  40. // pkg: test/hashmap
  41. // cpu: Intel(R) Core(TM) i7-4770HQ CPU @ 2.20GHz
  42. // BenchmarkGoSyncMap
  43. // BenchmarkGoSyncMap-8 1000000000 0.03480 ns/op 0 B/op 0 allocs/op
  44. // PASS
  45. // ok test/hashmap 2.544s
  46. mx := sync.Mutex{}
  47. m := map[string]int{}
  48. t.StartTimer()
  49. for i := 0; i < 100000; i++ {
  50. s := fmt.Sprint(i)
  51. mx.Lock()
  52. m[s] = i
  53. mx.Unlock()
  54. }
  55. t.StopTimer()
  56. }
  57. func BenchmarkSyncMap(t *testing.B) {
  58. // goos: darwin
  59. // goarch: amd64
  60. // pkg: test/hashmap
  61. // cpu: Intel(R) Core(TM) i7-4770HQ CPU @ 2.20GHz
  62. // BenchmarkSyncMap
  63. // BenchmarkSyncMap-8 1000000000 0.07106 ns/op 0 B/op 0 allocs/op
  64. // PASS
  65. // ok test/hashmap 1.068s
  66. m := sync.Map{}
  67. t.StartTimer()
  68. for i := 0; i < 100000; i++ {
  69. s := fmt.Sprint(i)
  70. m.Store(s, i)
  71. }
  72. t.StopTimer()
  73. }
  74. func BenchmarkCMap(t *testing.B) {
  75. // goos: darwin
  76. // goarch: amd64
  77. // pkg: test/hashmap
  78. // cpu: Intel(R) Core(TM) i7-4770HQ CPU @ 2.20GHz
  79. // BenchmarkCMap
  80. // BenchmarkCMap-8 1000000000 0.03648 ns/op 0 B/op 0 allocs/op
  81. // PASS
  82. // ok test/hashmap 0.769s
  83. m := cmap.New[string, any]()
  84. t.StartTimer()
  85. for i := 0; i < 100000; i++ {
  86. s := fmt.Sprint(i)
  87. m.Set(s, i)
  88. }
  89. t.StopTimer()
  90. }
  91. func BenchmarkHaxMap(t *testing.B) {
  92. // 1000000 loop
  93. // goos: darwin
  94. // goarch: amd64
  95. // pkg: test/hashmap
  96. // cpu: Intel(R) Core(TM) i7-4770HQ CPU @ 2.20GHz
  97. // BenchmarkHaxMap
  98. // BenchmarkHaxMap-8 1000000000 0.8453 ns/op 0 B/op 0 allocs/op
  99. // PASS
  100. // ok test/hashmap 65.175s
  101. // 100000 loop
  102. // goos: darwin
  103. // goarch: amd64
  104. // pkg: test/hashmap
  105. // cpu: Intel(R) Core(TM) i7-4770HQ CPU @ 2.20GHz
  106. // BenchmarkHaxMap
  107. // BenchmarkHaxMap-8 1000000000 0.05282 ns/op 0 B/op 0 allocs/op
  108. // PASS
  109. // ok test/hashmap 1.154s
  110. m := haxmap.New[string, int]()
  111. t.StartTimer()
  112. for i := 0; i < 100000; i++ {
  113. s := fmt.Sprint(i)
  114. m.Set(s, i)
  115. }
  116. t.StopTimer()
  117. }
  118. func BenchmarkHashMap(t *testing.B) {
  119. // 1000000 loop Crash
  120. // 100000 loop
  121. // goos: darwin
  122. // goarch: amd64
  123. // pkg: test/hashmap
  124. // cpu: Intel(R) Core(TM) i7-4770HQ CPU @ 2.20GHz
  125. // BenchmarkHashMap
  126. // BenchmarkHashMap-8 1 33365603372 ns/op 11402896 B/op 399822 allocs/op
  127. // PASS
  128. // ok test/hashmap 33.493s
  129. m := hashmap.New[string, int]()
  130. t.StartTimer()
  131. for i := 0; i < 100000; i++ {
  132. s := fmt.Sprint(i)
  133. m.Set(s, i)
  134. }
  135. t.StopTimer()
  136. }