structure/hashmap/hashmap_test.go

53 lines
935 B
Go
Raw Normal View History

2019-04-16 19:22:07 +00:00
package hashmap
2019-04-18 01:39:44 +00:00
import (
"fmt"
"runtime"
"testing"
"474420502.top/eson/structure/compare"
)
2019-04-16 19:22:07 +00:00
func TestCount(t *testing.T) {
2019-04-18 01:39:44 +00:00
hm := New(HashInt, compare.Int)
2019-04-21 05:31:39 +00:00
for i := 0; i < 1000; i++ {
2019-04-18 01:39:44 +00:00
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 BenchmarkCount(b *testing.B) {
hm := New(HashInt, compare.Int)
2019-04-21 05:31:39 +00:00
b.N = 100000
2019-04-18 01:39:44 +00:00
for i := 0; i < b.N; i++ {
hm.Put(i, i)
}
2019-04-21 05:31:39 +00:00
b.Log(len(hm.table), hm.size)
2019-04-18 01:39:44 +00:00
PrintMemUsage()
}
func BenchmarkGoCount(b *testing.B) {
m := make(map[int]int)
2019-04-21 05:31:39 +00:00
b.N = 100000
2019-04-18 01:39:44 +00:00
for i := 0; i < b.N; i++ {
m[i] = i
}
b.Log(len(m))
PrintMemUsage()
2019-04-16 19:22:07 +00:00
}