Last active
April 25, 2017 19:37
-
-
Save telendt/b7a9fc0524059dcbf0da1698a3142c98 to your computer and use it in GitHub Desktop.
Iterators benchmark
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
$ go test -bench=. | |
BenchmarkIterIndex-4 3000 478589 ns/op | |
BenchmarkIterIndexNoPanic-4 3000 469209 ns/op | |
BenchmarkIterSlice-4 2000 664497 ns/op | |
PASS | |
ok _/private/tmp/slice 5.353s |
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" | |
) | |
type iterator interface { | |
Next() bool | |
At() uint32 | |
} | |
type indexPostings struct { | |
list []uint32 | |
idx int | |
} | |
func newIndexPostings(list []uint32) *indexPostings { | |
return &indexPostings{list: list, idx: -1} | |
} | |
func (it *indexPostings) At() uint32 { | |
return it.list[it.idx] | |
} | |
func (it *indexPostings) Next() bool { | |
it.idx++ | |
return it.idx < len(it.list) | |
} | |
type noPanicIndexPostings struct { | |
list []uint32 | |
idx int | |
cur uint32 | |
} | |
func newNoPanicIndexPostings(list []uint32) *noPanicIndexPostings { | |
return &noPanicIndexPostings{list: list, idx: -1} | |
} | |
func (it *noPanicIndexPostings) At() uint32 { | |
return it.cur | |
} | |
func (it *noPanicIndexPostings) Next() bool { | |
it.idx++ | |
if it.idx < len(it.list) { | |
it.cur = it.list[it.idx] | |
return true | |
} | |
return false | |
} | |
type slicePostings struct { | |
list []uint32 | |
cur uint32 | |
} | |
func newSlicePostings(list []uint32) *slicePostings { | |
return &slicePostings{list: list} | |
} | |
func (it *slicePostings) At() uint32 { | |
return it.cur | |
} | |
func (it *slicePostings) Next() bool { | |
if len(it.list) > 0 { | |
it.cur = it.list[0] | |
it.list = it.list[1:] | |
return true | |
} | |
return false | |
} | |
func last(it iterator) uint32 { | |
var l uint32 | |
for it.Next() { | |
l = it.At() | |
} | |
return l | |
} | |
func Range(len int) []uint32 { | |
lst := make([]uint32, len) | |
for i := 0; i < len; i++ { | |
lst[i] = uint32(i) | |
} | |
return lst | |
} | |
var slice = Range(100000) | |
var result uint32 | |
func BenchmarkIterIndex(b *testing.B) { | |
var l uint32 | |
for i := 0; i < b.N; i++ { | |
l = last(newIndexPostings(slice)) | |
} | |
result = l | |
} | |
func BenchmarkIterIndexNoPanic(b *testing.B) { | |
var l uint32 | |
for i := 0; i < b.N; i++ { | |
l = last(newNoPanicIndexPostings(slice)) | |
} | |
result = l | |
} | |
func BenchmarkIterSlice(b *testing.B) { | |
var l uint32 | |
for i := 0; i < b.N; i++ { | |
l = last(newSlicePostings(slice)) | |
} | |
result = l | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment