Last active
November 21, 2017 16:12
-
-
Save gouthamve/f5b57ad12e24f5167e463c014d1914ec to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"sync/atomic" | |
) | |
type sample struct { | |
t int64 | |
v float64 | |
} | |
type simpleIterator struct { | |
vals []sample | |
i int | |
} | |
func newSimpleIterator(vals []sample) *simpleIterator { | |
return &simpleIterator{ | |
vals: vals, | |
i: -1, | |
} | |
} | |
func (si *simpleIterator) Next() bool { | |
si.i++ | |
return si.i < len(si.vals) | |
} | |
func (si *simpleIterator) At() (int64, float64) { | |
return si.vals[si.i].t, si.vals[si.i].v | |
} | |
func (si *simpleIterator) Reset() { | |
si.i = -1 | |
} | |
type countingIterator struct { | |
counter *int64 | |
it *simpleIterator | |
} | |
func newCountingIterator(c *int64, it *simpleIterator) *countingIterator { | |
return &countingIterator{c, it} | |
} | |
func (ci countingIterator) At() (int64, float64) { | |
atomic.AddInt64(ci.counter, 1) | |
return ci.it.At() | |
} | |
func (ci countingIterator) Next() bool { return ci.it.Next() } | |
func (ci countingIterator) Reset() { ci.it.Reset() } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "testing" | |
func benchmarkSimple(n int, b *testing.B) { | |
si := newSimpleIterator(generateSamples(n)) | |
b.ResetTimer() | |
for i := 0; i < b.N; i++ { | |
for si.Next() { | |
si.At() | |
} | |
si.Reset() | |
} | |
} | |
func benchmarkCounting(n int, b *testing.B) { | |
si := newSimpleIterator(generateSamples(n)) | |
var ctr int64 | |
ci := newCountingIterator(&ctr, si) | |
b.ResetTimer() | |
for i := 0; i < b.N; i++ { | |
for ci.Next() { | |
ci.At() | |
} | |
ci.Reset() | |
} | |
} | |
func BenchmarkSimple1000(b *testing.B) { benchmarkSimple(1000, b) } | |
func BenchmarkSimple10000(b *testing.B) { benchmarkSimple(10000, b) } | |
func BenchmarkSimple100000(b *testing.B) { benchmarkSimple(100000, b) } | |
func BenchmarkCounting1000(b *testing.B) { benchmarkCounting(1000, b) } | |
func BenchmarkCounting10000(b *testing.B) { benchmarkCounting(10000, b) } | |
func BenchmarkCounting100000(b *testing.B) { benchmarkCounting(100000, b) } | |
func generateSamples(n int) []sample { | |
s := make([]sample, 0, n) | |
for i := 0; i < n; i++ { | |
s = append(s, sample{}) | |
} | |
return s | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
➜ bench-atomic-iterator go test -bench=. | |
goos: linux | |
goarch: amd64 | |
pkg: github.com/gouthamve/x/bench-atomic-iterator | |
BenchmarkSimple1000-8 1000000 1475 ns/op | |
BenchmarkSimple10000-8 100000 14000 ns/op | |
BenchmarkSimple100000-8 10000 136397 ns/op | |
BenchmarkCounting1000-8 200000 6928 ns/op | |
BenchmarkCounting10000-8 20000 68760 ns/op | |
BenchmarkCounting100000-8 2000 679316 ns/op | |
PASS | |
ok github.com/gouthamve/x/bench-atomic-iterator 9.379s | |
============== NEW RESULTS BELOW =============== | |
➜ bench-atomic-iterator go test -bench=. | |
goos: linux | |
goarch: amd64 | |
pkg: github.com/gouthamve/x/bench-atomic-iterator | |
BenchmarkSimple1000-8 1384 ns/op | |
BenchmarkSimple10000-8 13729 ns/op | |
BenchmarkSimple100000-8 137595 ns/op | |
BenchmarkCounting1000-8 1837 ns/op | |
BenchmarkCounting10000-8 18678 ns/op | |
BenchmarkCounting100000-8 187173 ns/op | |
PASS | |
ok github.com/gouthamve/x/bench-atomic-iterator 10.118s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment