Skip to content

Instantly share code, notes, and snippets.

@bingoohuang
Last active May 10, 2019 06:03
Show Gist options
  • Save bingoohuang/e1f5de5d404c56096ebfc2652df22fc7 to your computer and use it in GitHub Desktop.
Save bingoohuang/e1f5de5d404c56096ebfc2652df22fc7 to your computer and use it in GitHub Desktop.
compare two style of copy golang slice performance
package gotrial
import (
"strings"
"testing"
)
func copyListV1(in []string) []string {
var out []string
for _, s := range in {
out = append(out, s)
}
return out
}
func copyListV2(in []string) []string {
out := make([]string, len(in))
for i, s := range in {
out[i] = s
}
return out
}
var input = strings.Fields(`Making code faster is exciting, and benchmarks in Go make that easy to do! Not really.
Optimizing a program can be complex and require careful consideration to do properly.
Daniel demonstrates techniques and tools which are a must for any performance aficionado.`)
func BenchmarkCopyList(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
copyListV2(input)
}
}
@bingoohuang
Copy link
Author

➜  gotrial export http_proxy=http://127.0.0.1:9999; export https_proxy=http://127.0.0.1:9999;
➜  gotrial go get golang.org/x/tools/cmd/benchcmp
go: finding golang.org/x/tools/cmd/benchcmp latest
go: finding golang.org/x/tools/cmd latest
go: finding golang.org/x/tools latest
go: downloading golang.org/x/tools v0.0.0-20190509153222-73554e0f7805
go: extracting golang.org/x/tools v0.0.0-20190509153222-73554e0f7805
➜  gotrial go test -bench=BenchmarkCopyList > v1.txt
➜  gotrial go test -bench=BenchmarkCopyList > v2.txt
➜  gotrial benchcmp v1.txt v2.txt
benchmark                old ns/op     new ns/op     delta
BenchmarkCopyList-12     703           190           -72.97%
benchmark                old allocs     new allocs     delta
BenchmarkCopyList-12     7              1              -85.71%
benchmark                old bytes     new bytes     delta
BenchmarkCopyList-12     2032          704           -65.35%
➜  gotrial go test -bench=. -cpuprofile=cpu.out
goos: darwin
goarch: amd64
pkg: gotrial
BenchmarkCopyList-12    	 2000000	       729 ns/op	    2032 B/op	       7 allocs/op
PASS
ok  	gotrial	2.372s
➜  gotrial
➜  gotrial go tool pprof cpu.out
Type: cpu
Time: May 10, 2019 at 10:53am (CST)
Duration: 2.36s, Total samples = 2.72s (115.09%)
Entering interactive mode (type "help" for commands, "o" for options)
(pprof) list gotrial.copyListV1
Total: 2.72s
ROUTINE ======================== gotrial.copyListV1 in /Users/bingoobjca/github/gotrial/cp_test.go
      10ms       80ms (flat, cum)  2.94% of Total
         .          .      6:)
         .          .      7:
         .          .      8:func copyListV1(in []string) []string {
         .          .      9:	var out []string
         .          .     10:
      10ms       10ms     11:	for _, s := range in {
         .       70ms     12:		out = append(out, s)
         .          .     13:	}
         .          .     14:
         .          .     15:	return out
         .          .     16:}
         .          .     17:
(pprof) %
➜  gotrial go test -bench=. -cpuprofile=cpu.out
goos: darwin
goarch: amd64
pkg: gotrial
BenchmarkCopyList-12    	10000000	       201 ns/op	     704 B/op	       1 allocs/op
PASS
ok  	gotrial	2.393s
➜  gotrial go tool pprof cpu.out
Type: cpu
Time: May 10, 2019 at 10:55am (CST)
Duration: 2.38s, Total samples = 2.88s (120.77%)
Entering interactive mode (type "help" for commands, "o" for options)
(pprof) list gotrial.copyListV2
Total: 2.88s
ROUTINE ======================== gotrial.copyListV2 in /Users/bingoobjca/github/gotrial/cp_test.go
      40ms       80ms (flat, cum)  2.78% of Total
         .          .     14:
         .          .     15:	return out
         .          .     16:}
         .          .     17:
         .          .     18:func copyListV2(in []string) []string {
         .       40ms     19:	out := make([]string, len(in))
         .          .     20:
         .          .     21:	for i, s := range in {
      40ms       40ms     22:		out[i] = s
         .          .     23:	}
         .          .     24:
         .          .     25:	return out
         .          .     26:}
         .          .     27:
(pprof) %

@bingoohuang
Copy link
Author

@bingoohuang
Copy link
Author

➜  gotrial go get golang.org/x/perf/cmd/benchstat
➜  gotrial benchstat v1.txt v2.txt
name         old time/op    new time/op    delta
CopyList-12     703ns ± 0%     190ns ± 0%   ~     (p=1.000 n=1+1)

name         old alloc/op   new alloc/op   delta
CopyList-12    2.03kB ± 0%    0.70kB ± 0%   ~     (p=1.000 n=1+1)

name         old allocs/op  new allocs/op  delta
CopyList-12      7.00 ± 0%      1.00 ± 0%   ~     (p=1.000 n=1+1)

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