Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The performance of swiss Get() is worse than go map at capacity 1~500. #11

Open
ankisme opened this issue Apr 8, 2023 · 0 comments
Open

Comments

@ankisme
Copy link

ankisme commented Apr 8, 2023

Here is the benchmark.

I found the performance of Get() method is worse than go map get when the capacity is 1~500.

And in my project, call times of Get() method is much more than Put(). And the capacity of map is often less than 500.

So the performance of swiss is not good enough. Wish to improve it.

goos: windows
goarch: amd64
pkg: web/util/listutil
cpu: Intel(R) Core(TM) i7-7700K CPU @ 4.20GHz
BenchmarkCreateSwissMap
BenchmarkCreateSwissMap-8         499999              3184 ns/op           10128
 B/op          3 allocs/op
BenchmarkCreateGoMap
BenchmarkCreateGoMap-8            249999              5692 ns/op           20504
 B/op          2 allocs/op
BenchmarkSwissMapPut
BenchmarkSwissMapPut-8             35820             37102 ns/op           34624
 B/op         15 allocs/op
BenchmarkGoMapPut
BenchmarkGoMapPut-8                28435             44501 ns/op           43173
 B/op         42 allocs/op
BenchmarkSwissMapGet
BenchmarkSwissMapGet-8            226414              5092 ns/op               0
 B/op          0 allocs/op
BenchmarkGoMapGet
BenchmarkGoMapGet-8               292682              4278 ns/op               0
 B/op          0 allocs/op
BenchmarkSwissMapIterate
BenchmarkSwissMapIterate-8        750000              1616 ns/op               0
 B/op          0 allocs/op
BenchmarkGoMapIterate
BenchmarkGoMapIterate-8           249999              4900 ns/op               0
 B/op          0 allocs/op
PASS
package listutil_test

import (
	"github.com/dolthub/swiss"
	"testing"
)

const capacity = 500

func CreateSwissMap() *swiss.Map[int, int] {
	m := swiss.NewMap[int, int](0)

	for i := 1; i <= capacity; i++ {
		m.Put(i, i)
	}

	return m
}

func CreateMap() map[int]int {
	m := map[int]int{}

	for i := 1; i <= capacity; i++ {
		m[i] = i
	}

	return m
}

func BenchmarkCreateSwissMap(b *testing.B) {
	b.ReportAllocs()
	b.ResetTimer()

	for n := 0; n < b.N; n++ {
		m := swiss.NewMap[int, int](capacity)
		if m != nil {

		}
	}
}

func BenchmarkCreateGoMap(b *testing.B) {
	b.ReportAllocs()
	b.ResetTimer()

	for n := 0; n < b.N; n++ {
		m := make(map[int]int, capacity)
		if m != nil {

		}
	}
}

func BenchmarkSwissMapPut(b *testing.B) {
	b.ReportAllocs()
	b.ResetTimer()

	for n := 0; n < b.N; n++ {
		CreateSwissMap()
	}
}

func BenchmarkGoMapPut(b *testing.B) {
	b.ReportAllocs()
	b.ResetTimer()

	for n := 0; n < b.N; n++ {
		CreateMap()
	}
}

func BenchmarkSwissMapGet(b *testing.B) {
	m := CreateSwissMap()
	size := m.Count()

	b.ReportAllocs()
	b.ResetTimer()

	for n := 0; n < b.N; n++ {
		for i := 1; i <= size; i++ {
			v, ok := m.Get(i)
			if !ok || v == 0 {
				panic(`not found`)
			}
		}
	}
}

func BenchmarkGoMapGet(b *testing.B) {
	m := CreateMap()
	size := len(m)

	b.ReportAllocs()
	b.ResetTimer()

	for n := 0; n < b.N; n++ {
		for i := 1; i <= size; i++ {
			v, ok := m[i]
			if !ok || v == 0 {
				panic(`not found`)
			}
		}
	}
}

func BenchmarkSwissMapIterate(b *testing.B) {
	m := CreateSwissMap()

	b.ReportAllocs()
	b.ResetTimer()

	for n := 0; n < b.N; n++ {
		m.Iter(func(k int, v int) (stop bool) {
			if k <= 0 {
				panic(1)
			}

			if v == 0 {
				panic(1)
			}

			return false
		})
	}
}

func BenchmarkGoMapIterate(b *testing.B) {
	m := CreateMap()

	b.ReportAllocs()
	b.ResetTimer()

	for n := 0; n < b.N; n++ {
		for k, v := range m {
			if k <= 0 {
				panic(1)
			}

			if v == 0 {
				panic(1)
			}
		}
	}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant