benchmarks!

master
magical 2022-01-23 00:23:45 +00:00
parent d27d5348f9
commit 9ed6f8c536
1 changed files with 90 additions and 2 deletions

View File

@ -5,16 +5,104 @@ import "testing"
func TestPmap(t *testing.T) { func TestPmap(t *testing.T) {
p := New() p := New()
const numElems = 100 const numElems = 100
for i := range make([]int, numElems) { for i := range iter(numElems) {
p = p.Set(i, i) p = p.Set(i, i)
} }
if p.Len() != numElems { if p.Len() != numElems {
t.Fatalf("Len() = %v, want %v", p.Len(), numElems) t.Fatalf("Len() = %v, want %v", p.Len(), numElems)
} }
for i := range make([]int, numElems) { for i := range iter(numElems) {
v, ok := p.Get(i) v, ok := p.Get(i)
if v != i || !ok { if v != i || !ok {
t.Errorf("Get(%d) = %v %v, want %v %v", i, v, ok, i, true) t.Errorf("Get(%d) = %v %v, want %v %v", i, v, ok, i, true)
} }
} }
} }
func BenchmarkGet(b *testing.B) {
b.Run("size=10", func(b *testing.B) { benchmarkGet(b, 10) })
b.Run("size=100", func(b *testing.B) { benchmarkGet(b, 100) })
b.Run("size=1000", func(b *testing.B) { benchmarkGet(b, 1000) })
b.Run("size=10000", func(b *testing.B) { benchmarkGet(b, 10000) })
}
func BenchmarkGetAbsent(b *testing.B) {
b.Run("size=10", func(b *testing.B) { benchmarkGetAbsent(b, 10) })
b.Run("size=100", func(b *testing.B) { benchmarkGetAbsent(b, 100) })
b.Run("size=1000", func(b *testing.B) { benchmarkGetAbsent(b, 1000) })
b.Run("size=10000", func(b *testing.B) { benchmarkGetAbsent(b, 10000) })
}
func benchmarkGet(b *testing.B, numElems int) {
p := New()
for i := range iter(numElems) {
p = p.Set(i, i)
}
if p.Len() != numElems {
b.Fatalf("Len() = %v, want %v", p.Len(), numElems)
}
b.ResetTimer()
i := 0
for range iter(b.N) {
v, ok := p.Get(i)
if v != i || !ok {
b.Errorf("Get(%d) = %v %v, want %v %v", i, v, ok, i, true)
}
i++
if i >= numElems {
i = 0
}
}
}
func benchmarkGetAbsent(b *testing.B, numElems int) {
p := New()
for i := range iter(numElems) {
p = p.Set(-i-1, i)
}
if p.Len() != numElems {
b.Fatalf("Len() = %v, want %v", p.Len(), numElems)
}
b.ResetTimer()
i := 0
for range iter(b.N) {
v, ok := p.Get(i)
if ok {
b.Errorf("Get(%d) = %v %v, want %v %v", i, v, ok, 0, false)
}
i++
}
}
func iter(n int) []struct{} {
return make([]struct{}, n)
}
func BenchmarkHmapGet_baseline(b *testing.B) {
b.Run("size=10", func(b *testing.B) { benchmarkHmapGet(b, 10) })
b.Run("size=100", func(b *testing.B) { benchmarkHmapGet(b, 100) })
b.Run("size=1000", func(b *testing.B) { benchmarkHmapGet(b, 1000) })
b.Run("size=10000", func(b *testing.B) { benchmarkHmapGet(b, 10000) })
}
func benchmarkHmapGet(b *testing.B, numElems int) {
h := make(map[int]int)
for i := range iter(numElems) {
h[i] = i
}
if len(h) != numElems {
b.Fatalf("Len() = %v, want %v", len(h), numElems)
}
b.ResetTimer()
i := 0
for range iter(b.N) {
v, ok := h[i]
if v != i || !ok {
b.Errorf("h[%d] = %v %v, want %v %v", i, v, ok, i, true)
}
i++
if i >= numElems {
i = 0
}
}
}