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

iterating over map is 10x slower #9

Closed
korniltsev opened this issue Jan 5, 2023 · 3 comments
Closed

iterating over map is 10x slower #9

korniltsev opened this issue Jan 5, 2023 · 3 comments

Comments

@korniltsev
Copy link
Contributor

func BenchmarkIntIntMapIterKeys(b *testing.B) {
	var j, v, sum int64
	var ok bool
	m := New(2048, 0.60)
	fillIntIntMap(m)
	for i := 0; i < b.N; i++ {
		sum = int64(0)
		for j = range m.Keys() {
			if v, ok = m.Get(j); ok {
				sum += j
				sum += v
			}
		}
		//log.Println("int int sum:", sum)
	}
}

func BenchmarkIntIntMapIterItems(b *testing.B) {
	var j, v, sum int64
	var kv [2]int64
	m := New(2048, 0.60)
	fillIntIntMap(m)
	for i := 0; i < b.N; i++ {
		sum = int64(0)
		for kv = range m.Items() {
			j = kv[0]
			v = kv[1]
			sum += j
			sum += v
		}
		//log.Println("int int sum:", sum)
	}
}

func BenchmarkIntIntMapIterNoChan(b *testing.B) {
	var sum int64
	m := New(2048, 0.60)
	fillIntIntMap(m)
	for i := 0; i < b.N; i++ {
		sum = int64(0)
		m.Items2(func(k, v int64) {
			sum += k
			sum += v
		})

		//log.Println("int int sum:", sum)
	}
}

func BenchmarkStdMapIter(b *testing.B) {
	var j, v, sum int64
	m := make(map[int64]int64, 2048)
	fillStdMap(m)
	for i := 0; i < b.N; i++ {
		sum = int64(0)
		for j, v = range m {
			sum += j
			sum += v
		}
		//log.Println("map sum:", sum)
	}
}
cpu: AMD Ryzen 9 5950X 16-Core Processor            
BenchmarkIntIntMapIterKeys
BenchmarkIntIntMapIterKeys-32      	       9	 119676935 ns/op
BenchmarkIntIntMapIterItems
BenchmarkIntIntMapIterItems-32     	       8	 127800299 ns/op
BenchmarkIntIntMapIterNoChan
BenchmarkIntIntMapIterNoChan-32    	      76	  14489078 ns/op
BenchmarkStdMapIter
BenchmarkStdMapIter-32             	      69	  15238876 ns/op
@brentp
Copy link
Owner

brentp commented Jan 7, 2023

If I understand correctly, you're showing that iterating over a(n intint)map using a channel is much slower.
care to make a PR with Items2? Perhaps each can name it Each ?

@korniltsev
Copy link
Contributor Author

@brentp ptal #10

@brentp
Copy link
Owner

brentp commented Jan 8, 2023

closed by #10

@brentp brentp closed this as completed Jan 8, 2023
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

2 participants