Skip to content

Instantly share code, notes, and snippets.

@timakin
Created July 28, 2017 06:33
Show Gist options
  • Save timakin/dbeaf2bdbd37bc8c20aabac6d9fab535 to your computer and use it in GitHub Desktop.
Save timakin/dbeaf2bdbd37bc8c20aabac6d9fab535 to your computer and use it in GitHub Desktop.
package main
func main() {
PopWithCopy()
}
func PopWithCopy() string {
strs := []string{
"AAAAAAAAAAA",
"AAAAAAAAAAA",
"AAAAAAAAAAA",
"AAAAAAAAAAA",
"AAAAAAAAAAA",
"AAAAAAAAAAA",
"AAAAAAAAAAA",
"AAAAAAAAAAA",
"AAAAAAAAAAA",
"AAAAAAAAAAA",
}
p := strs[0]
copy(strs, strs[1:])
return p
}
func PopWithEqual() string {
strs := []string{
"AAAAAAAAAAA",
"AAAAAAAAAAA",
"AAAAAAAAAAA",
"AAAAAAAAAAA",
"AAAAAAAAAAA",
"AAAAAAAAAAA",
"AAAAAAAAAAA",
"AAAAAAAAAAA",
"AAAAAAAAAAA",
"AAAAAAAAAAA",
}
p, strs := strs[0], strs[1:]
return p
}
package main
import (
"testing"
)
func Benchmark_NormalPop(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
PopWithCopy()
}
}
func Benchmark_EqualPop(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
PopWithEqual()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment