Last active
March 12, 2020 14:52
-
-
Save lavoiesl/b416c132fbd74e5aff0ec2f25516d785 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 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) | |
} | |
} |
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
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