Skip to content

Instantly share code, notes, and snippets.

@evalphobia
Created November 30, 2016 01:58
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 evalphobia/c239d5af579bd72fb61a6da4ab2eacbb to your computer and use it in GitHub Desktop.
Save evalphobia/c239d5af579bd72fb61a6da4ab2eacbb to your computer and use it in GitHub Desktop.
[Golang] benchmark for unshifting to slice operation
package main
import (
"testing"
)
type User struct {
ID int
a int
b int64
c int
d string
e bool
f float64
g int
h int
i int
}
var (
UserList []User
UserPointerList []*User
intList []int
)
const defaultSize = 60000
func ready() {
UserList = make([]User, defaultSize)
UserPointerList = make([]*User, defaultSize)
intList = make([]int, defaultSize)
for i := 0; i < len(UserList); i++ {
UserList[i].ID = i
UserPointerList[i] = &User{ID: i}
intList[i] = i
}
}
func BenchmarkUserListYamashita(b *testing.B) {
ready()
b.ResetTimer()
for i := 0; i < b.N; i++ {
result := userListYamashita()
_ = result
// assertResult(len(result))
}
}
func userListYamashita() []User {
tmpUserList := make([]User, len(UserList)+1)
tmpUserList[0] = User{ID: -1}
for i := range UserList {
tmpUserList[i+1] = UserList[i]
}
return tmpUserList
}
func BenchmarkUserListNormal(b *testing.B) {
ready()
b.ResetTimer()
for i := 0; i < b.N; i++ {
result := userListNormal()
_ = result
// assertResult(len(result))
}
}
func userListNormal() []User {
return append([]User{
User{ID: -1},
}, UserList...)
}
func BenchmarkUserPointerListYamashita(b *testing.B) {
ready()
b.ResetTimer()
for i := 0; i < b.N; i++ {
result := userPointerListYamashita()
_ = result
// assertResult(len(result))
}
}
func userPointerListYamashita() []*User {
tmpUserList := make([]*User, len(UserPointerList)+1)
tmpUserList[0] = &User{ID: -1}
for i := range UserPointerList {
tmpUserList[i+1] = UserPointerList[i]
}
return tmpUserList
}
func BenchmarkUserPointerListNormal(b *testing.B) {
ready()
b.ResetTimer()
for i := 0; i < b.N; i++ {
result := userPointerListNormal()
_ = result
// assertResult(len(result))
}
}
func userPointerListNormal() []*User {
return append([]*User{
&User{ID: -1},
}, UserPointerList...)
}
func BenchmarkIntListYamashita(b *testing.B) {
ready()
b.ResetTimer()
for i := 0; i < b.N; i++ {
result := intListYamashita()
_ = result
// assertResult(len(result))
}
}
func intListYamashita() []int {
tmpIntList := make([]int, len(intList)+1)
tmpIntList[0] = -1
for i := range intList {
tmpIntList[i+1] = intList[i]
}
return tmpIntList
}
func BenchmarkIntListYamashita2(b *testing.B) {
ready()
b.ResetTimer()
for i := 0; i < b.N; i++ {
result := intListYamashita2()
_ = result
// assertResult(len(result))
}
}
func intListYamashita2() []int {
tmpIntList := make([]int, len(intList)+1)
tmpIntList[0] = -1
for i, max := 0, len(intList); i < max; i++ {
tmpIntList[i+1] = intList[i]
}
return tmpIntList
}
func BenchmarkIntListNormal(b *testing.B) {
ready()
b.ResetTimer()
for i := 0; i < b.N; i++ {
result := intListNormal()
_ = result
// assertResult(len(result))
}
}
func intListNormal() []int {
return append([]int{-1}, intList...)
}
func assertResult(size int) {
if size != defaultSize+1 {
panic("error assertResult")
}
}
@evalphobia
Copy link
Author

$ go test -bench=. -benchmem

testing: warning: no tests to run
BenchmarkUserListYamashita-4          	     200	   5058534 ns/op	 5283849 B/op	       1 allocs/op
BenchmarkUserListNormal-4             	     300	   5407515 ns/op	 5283840 B/op	       1 allocs/op
BenchmarkUserPointerListYamashita-4   	    5000	    376403 ns/op	  483424 B/op	       2 allocs/op
BenchmarkUserPointerListNormal-4      	    3000	    490639 ns/op	  483424 B/op	       2 allocs/op
BenchmarkIntListYamashita-4           	   10000	    260298 ns/op	  483328 B/op	       1 allocs/op
BenchmarkIntListYamashita2-4          	    5000	    261580 ns/op	  483328 B/op	       1 allocs/op
BenchmarkIntListNormal-4              	    5000	    231447 ns/op	  483328 B/op	       1 allocs/op

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment