Skip to content

Instantly share code, notes, and snippets.

@lavoiesl
Last active March 12, 2020 14:52
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 lavoiesl/b416c132fbd74e5aff0ec2f25516d785 to your computer and use it in GitHub Desktop.
Save lavoiesl/b416c132fbd74e5aff0ec2f25516d785 to your computer and use it in GitHub Desktop.
package main
import "testing"
var list []*struct{}
func init() {
for i := 0; i < 1000; i++ {
list = append(list, nil)
}
}
func inPlace(input []*struct{}) []*struct{} {
n := 0
for _, i := range input {
input[n] = i
n++
}
return input[:n]
}
func newSlice(input []*struct{}) (output []*struct{}) {
for _, i := range input {
output = append(output, i)
}
return output
}
func Benchmark_Inplace(b *testing.B) {
for n := 0; n < b.N; n++ {
inPlace(list)
}
}
func Benchmark_NewSlice(b *testing.B) {
for n := 0; n < b.N; n++ {
newSlice(list)
}
}
Benchmark_Inplace-8 3655371 325 ns/op 0 B/op 0 allocs/op
Benchmark_NewSlice-8 135073 8602 ns/op 16376 B/op 11 allocs/op
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment