135 lines
2.1 KiB
Go
135 lines
2.1 KiB
Go
package hashmap
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/gob"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"log"
|
|
"runtime"
|
|
"testing"
|
|
|
|
"474420502.top/eson/structure/compare"
|
|
)
|
|
|
|
func loadTestData() []int {
|
|
log.SetFlags(log.Lshortfile)
|
|
|
|
data, err := ioutil.ReadFile("../l.log")
|
|
if err != nil {
|
|
log.Println(err)
|
|
}
|
|
var l []int
|
|
decoder := gob.NewDecoder(bytes.NewReader(data))
|
|
decoder.Decode(&l)
|
|
return l
|
|
}
|
|
func TestCount(t *testing.T) {
|
|
hm := New(HashInt, compare.Int)
|
|
|
|
for i := 0; i < 100000; i++ {
|
|
hm.Put(i, i)
|
|
}
|
|
|
|
// t.Error(hm.Get(4))
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
var executeCount = 5
|
|
var compareSize = 100000
|
|
|
|
func BenchmarkPut(b *testing.B) {
|
|
b.StopTimer()
|
|
|
|
l := loadTestData()
|
|
hm := New(HashInt, compare.Int)
|
|
b.N = len(l) * executeCount
|
|
for i := 0; i < len(l); i++ {
|
|
v := l[i]
|
|
hm.Put(v, v)
|
|
}
|
|
|
|
b.StartTimer()
|
|
|
|
for c := 0; c < executeCount; c++ {
|
|
for i := 0; i < len(l); i++ {
|
|
v := l[i]
|
|
hm.Put(v, v)
|
|
}
|
|
}
|
|
|
|
//b.Log(len(hm.table), hm.size)
|
|
//PrintMemUsage()
|
|
}
|
|
|
|
func BenchmarkGoPut(b *testing.B) {
|
|
|
|
l := loadTestData()
|
|
|
|
hm := make(map[int]int)
|
|
b.N = len(l) * executeCount
|
|
for c := 0; c < executeCount; c++ {
|
|
for i := 0; i < len(l); i++ {
|
|
v := l[i]
|
|
hm[v] = v
|
|
}
|
|
}
|
|
|
|
//b.Log(len(m))
|
|
//PrintMemUsage()
|
|
}
|
|
|
|
func BenchmarkGet(b *testing.B) {
|
|
|
|
b.StopTimer()
|
|
l := loadTestData()
|
|
hm := New(HashInt, compare.Int)
|
|
b.N = len(l) * executeCount
|
|
for i := 0; i < len(l); i++ {
|
|
v := l[i]
|
|
hm.Put(v, v)
|
|
}
|
|
b.StartTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
hm.Get(i)
|
|
}
|
|
|
|
//b.Log(len(hm.table), hm.size)
|
|
//PrintMemUsage()
|
|
}
|
|
|
|
func BenchmarkGoGet(b *testing.B) {
|
|
b.StopTimer()
|
|
l := loadTestData()
|
|
hm := make(map[int]int)
|
|
b.N = len(l) * executeCount
|
|
|
|
for i := 0; i < len(l); i++ {
|
|
v := l[i]
|
|
hm[v] = v
|
|
}
|
|
|
|
b.StartTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
if _, ok := hm[i]; !ok {
|
|
|
|
}
|
|
}
|
|
//b.Log(len(m))
|
|
//PrintMemUsage()
|
|
}
|