$ benchcmp old.txt new.txt
benchmark old ns/op new ns/op delta
BenchmarkBy*-4 319 99.4 -68.84%
BenchmarkBy*-4 318 138 -56.60%
BenchmarkBy*-4 321 88.7 -72.37%
BenchmarkBy*-4 320 90.2 -71.81%
BenchmarkBy*-4 319 85.9 -73.07%
benchmark old allocs new allocs delta
BenchmarkBy*-4 4 1 -75.00%
BenchmarkBy*-4 4 1 -75.00%
BenchmarkBy*-4 4 1 -75.00%
BenchmarkBy*-4 4 1 -75.00%
BenchmarkBy*-4 4 1 -75.00%
benchmark old bytes new bytes delta
BenchmarkBy*-4 240 80 -66.67%
BenchmarkBy*-4 240 80 -66.67%
BenchmarkBy*-4 240 80 -66.67%
BenchmarkBy*-4 240 80 -66.67%
BenchmarkBy*-4 240 80 -66.67%
Created
October 27, 2017 21:58
-
-
Save odeke-em/0e068d49dde2dcfa087db81c07611479 to your computer and use it in GitHub Desktop.
Exhibit of the advantage of copy vs append
This file contains hidden or 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 ( | |
"fmt" | |
"testing" | |
) | |
var sample = []interface{}{"a", 1, 20.9, fmt.Println, 'Z'} | |
func byCopy(il []interface{}) []interface{} { | |
icl := make([]interface{}, len(il)) | |
copy(icl, il) | |
return icl | |
} | |
func byAppend(il []interface{}) []interface{} { | |
var icl []interface{} | |
for _, iv := range il { | |
icl = append(icl, iv) | |
} | |
return icl | |
} | |
func BenchmarkByAppend(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
if tl := byAppend(sample); tl != nil { | |
} | |
} | |
b.ReportAllocs() | |
} | |
func BenchmarkByCopy(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
if tl := byCopy(sample); tl != nil { | |
} | |
} | |
b.ReportAllocs() | |
} |
This file contains hidden or 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
BenchmarkBy*-4 20000000 99.4 ns/op 80 B/op 1 allocs/op | |
BenchmarkBy*-4 10000000 138 ns/op 80 B/op 1 allocs/op | |
BenchmarkBy*-4 20000000 88.7 ns/op 80 B/op 1 allocs/op | |
BenchmarkBy*-4 20000000 90.2 ns/op 80 B/op 1 allocs/op | |
BenchmarkBy*-4 20000000 85.9 ns/op 80 B/op 1 allocs/op |
This file contains hidden or 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
BenchmarkBy*-4 5000000 319 ns/op 240 B/op 4 allocs/op | |
BenchmarkBy*-4 5000000 318 ns/op 240 B/op 4 allocs/op | |
BenchmarkBy*-4 5000000 321 ns/op 240 B/op 4 allocs/op | |
BenchmarkBy*-4 5000000 320 ns/op 240 B/op 4 allocs/op | |
BenchmarkBy*-4 5000000 319 ns/op 240 B/op 4 allocs/op |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment