Skip to content

Instantly share code, notes, and snippets.

@mattn
Last active September 28, 2015 05:36
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 mattn/77952b5f5a2ba2e167e6 to your computer and use it in GitHub Desktop.
Save mattn/77952b5f5a2ba2e167e6 to your computer and use it in GitHub Desktop.
package foo
import (
"testing"
)
type comment struct {
ID int
}
var (
comments []comment
)
func ready() {
comments = make([]comment, 60000)
for i := 0; i < len(comments); i++ {
comments[i].ID = i
}
}
func BenchmarkTest1(b *testing.B) {
ready()
b.ResetTimer()
hoge := []int{}
for _, comment := range comments {
hoge = append([]int{comment.ID}, hoge...)
}
}
func BenchmarkTest2(b *testing.B) {
ready()
b.ResetTimer()
hoge := make([]int, len(comments))
l := len(comments)
for i := l - 1; i >= 0; i-- {
hoge[i] = comments[i].ID
}
}
testing: warning: no tests to run
PASS
BenchmarkTest1-4 1 11059105800 ns/op
BenchmarkTest2-4 2000000000 0.00 ns/op
ok _/c_/dev/arrr 11.519s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment