Skip to content

Instantly share code, notes, and snippets.

@nu7hatch
Last active December 19, 2015 11:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nu7hatch/5945218 to your computer and use it in GitHub Desktop.
Save nu7hatch/5945218 to your computer and use it in GitHub Desktop.
chris@abyss · 1.9.3 · /tmp/bench_list
$ time go run slice.go
real 0m0.858s
user 0m0.732s
sys 0m0.112s
chris@abyss · 1.9.3 · /tmp/bench_list
$ time go run list.go
real 0m1.556s
user 0m1.376s
sys 0m0.164s
package main
import "container/list"
type bin struct {
value float64
count float64
}
func main() {
l := list.New()
for i := 0; i <= 5000000; i++ {
l.PushFront(&bin{1,2})
}
for e := l.Front(); e != nil; e = e.Next() {
_ = e.Value.(*bin)
}
}
package main
type bin struct {
value float64
count float64
}
type BinList []*bin
func (l *BinList) Append(b *bin) {
(*l) = append((*l), b)
}
func (l *BinList) Len() int {
return len(*l)
}
func (l *BinList) ValueOf(i int) *bin {
return (*l)[i]
}
func (l *BinList) Each(f func(*bin)) {
for _, b := range (*l) {
f(b)
}
}
func main() {
l := BinList(make([]*bin, 0))
for i := 0; i <= 5000000; i++ {
l.Append(&bin{1,2})
}
for i := 0; i < l.Len(); i++ {
_ = l.ValueOf(i)
}
// slightly slower that traditional iterating over list elements (100ms of
// difference only though).
//l.Each(func(b *bin) {
// _ = b
//})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment