Last active
September 28, 2015 05:36
-
-
Save mattn/77952b5f5a2ba2e167e6 to your computer and use it in GitHub Desktop.
This file contains 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 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 | |
} | |
} |
This file contains 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
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