Skip to content

Instantly share code, notes, and snippets.

@odeke-em
Last active March 2, 2018 20:51
Show Gist options
  • Save odeke-em/f8fb3a8695b33dd06a700fc3473f3e62 to your computer and use it in GitHub Desktop.
Save odeke-em/f8fb3a8695b33dd06a700fc3473f3e62 to your computer and use it in GitHub Desktop.
Benchmarking invoking defer fn() vs fn()
package main
import (
"sync"
"testing"
)
type st struct {
sync.Mutex
i int
}
func (c *st) callDefer() error {
c.Lock()
defer c.Unlock()
c.i += 1
return nil
}
func (c *st) callNoDefer() error {
c.Lock()
c.i += 1
c.Unlock()
return nil
}
func BenchmarkNoDefer(b *testing.B) {
var err error
it := new(st)
for i := 0; i < b.N; i++ {
err = it.callNoDefer()
}
b.ReportAllocs()
if err != nil {
}
}
func BenchmarkDefer(b *testing.B) {
var err error
it := new(st)
for i := 0; i < b.N; i++ {
err = it.callDefer()
}
b.ReportAllocs()
if err != nil {
}
}
$ benchstat before.txt after.txt 
name     old time/op    new time/op    delta
Defer-4    23.1ns ± 3%    51.2ns ± 1%  +121.22%        (p=0.000 n=10+10)

name     old alloc/op   new alloc/op   delta
Defer-4    0.00B ±NaN%    0.00B ±NaN%      ~     (all samples are equal)

name     old allocs/op  new allocs/op  delta
Defer-4     0.00 ±NaN%     0.00 ±NaN%      ~     (all samples are equal)

where:

  • before.txt -- without defer
  • after.txt -- with defer
$ go version && go test -v -run=^$ -bench=. -count=10
go version devel +f612cd7 Thu Aug 10 01:35:28 2017 +0000 darwin/amd64
goos: darwin
goarch: amd64
BenchmarkNoDefer-4   	100000000	        23.3 ns/op	       0 B/op	       0 allocs/op
BenchmarkNoDefer-4   	100000000	        23.3 ns/op	       0 B/op	       0 allocs/op
BenchmarkNoDefer-4   	100000000	        23.3 ns/op	       0 B/op	       0 allocs/op
BenchmarkNoDefer-4   	100000000	        23.9 ns/op	       0 B/op	       0 allocs/op
BenchmarkNoDefer-4   	100000000	        23.0 ns/op	       0 B/op	       0 allocs/op
BenchmarkNoDefer-4   	100000000	        22.8 ns/op	       0 B/op	       0 allocs/op
BenchmarkNoDefer-4   	100000000	        22.8 ns/op	       0 B/op	       0 allocs/op
BenchmarkNoDefer-4   	100000000	        23.4 ns/op	       0 B/op	       0 allocs/op
BenchmarkNoDefer-4   	100000000	        22.9 ns/op	       0 B/op	       0 allocs/op
BenchmarkNoDefer-4   	100000000	        22.7 ns/op	       0 B/op	       0 allocs/op
BenchmarkDefer-4     	30000000	        51.5 ns/op	       0 B/op	       0 allocs/op
BenchmarkDefer-4     	30000000	        51.0 ns/op	       0 B/op	       0 allocs/op
BenchmarkDefer-4     	30000000	        51.1 ns/op	       0 B/op	       0 allocs/op
BenchmarkDefer-4     	30000000	        51.4 ns/op	       0 B/op	       0 allocs/op
BenchmarkDefer-4     	30000000	        51.5 ns/op	       0 B/op	       0 allocs/op
BenchmarkDefer-4     	30000000	        50.9 ns/op	       0 B/op	       0 allocs/op
BenchmarkDefer-4     	30000000	        51.2 ns/op	       0 B/op	       0 allocs/op
BenchmarkDefer-4     	30000000	        50.8 ns/op	       0 B/op	       0 allocs/op
BenchmarkDefer-4     	30000000	        50.6 ns/op	       0 B/op	       0 allocs/op
BenchmarkDefer-4     	30000000	        51.9 ns/op	       0 B/op	       0 allocs/op
PASS
ok  	_/Users/emmanuelodeke/Desktop/benchmarking/defers	39.249s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment