91 lines
1.5 KiB
Go
91 lines
1.5 KiB
Go
package hashmap
|
|
|
|
import (
|
|
"fmt"
|
|
"runtime"
|
|
"testing"
|
|
|
|
"474420502.top/eson/structure/compare"
|
|
)
|
|
|
|
func TestCount(t *testing.T) {
|
|
hm := New(HashInt, compare.Int)
|
|
|
|
for i := 0; i < 1000; i++ {
|
|
hm.Put(i, i)
|
|
}
|
|
}
|
|
|
|
func PrintMemUsage() {
|
|
var m runtime.MemStats
|
|
runtime.ReadMemStats(&m)
|
|
// For info on each, see: https://golang.org/pkg/runtime/#MemStats
|
|
fmt.Printf("Alloc = %v MiB", bToMb(m.Alloc))
|
|
fmt.Printf("\tTotalAlloc = %v MiB", bToMb(m.TotalAlloc))
|
|
fmt.Printf("\tSys = %v MiB", bToMb(m.Sys))
|
|
fmt.Printf("\tNumGC = %v\n", m.NumGC)
|
|
}
|
|
|
|
func bToMb(b uint64) uint64 {
|
|
return b / 1024 / 1024
|
|
}
|
|
|
|
func BenchmarkPut(b *testing.B) {
|
|
hm := New(HashInt, compare.Int)
|
|
b.N = 100000
|
|
for i := 0; i < b.N; i++ {
|
|
hm.Put(i, i)
|
|
}
|
|
|
|
//b.Log(len(hm.table), hm.size)
|
|
//PrintMemUsage()
|
|
}
|
|
|
|
func BenchmarkGet(b *testing.B) {
|
|
b.StopTimer()
|
|
hm := New(HashInt, compare.Int)
|
|
b.N = 100000
|
|
for i := 0; i < b.N; i++ {
|
|
hm.Put(i, i)
|
|
}
|
|
b.StartTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
hm.Get(i)
|
|
}
|
|
|
|
//b.Log(len(hm.table), hm.size)
|
|
//PrintMemUsage()
|
|
}
|
|
|
|
func BenchmarkGoPut(b *testing.B) {
|
|
m := make(map[int]int)
|
|
b.N = 100000
|
|
for i := 0; i < b.N; i++ {
|
|
m[i] = i
|
|
}
|
|
//b.Log(len(m))
|
|
//PrintMemUsage()
|
|
}
|
|
|
|
func BenchmarkGoGet(b *testing.B) {
|
|
b.StopTimer()
|
|
m := make(map[int]int)
|
|
b.N = 100000
|
|
for i := 0; i < b.N; i++ {
|
|
m[i] = i
|
|
}
|
|
for i := 0; i < b.N; i++ {
|
|
if _, ok := m[i]; !ok {
|
|
|
|
}
|
|
}
|
|
b.StartTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
if _, ok := m[i]; !ok {
|
|
|
|
}
|
|
}
|
|
//b.Log(len(m))
|
|
//PrintMemUsage()
|
|
}
|