3 ms·
I haven't tested this since then, but this wasn't the case in 2014[1]. I haven't noticed these issues since then, but back then it cost hundreds of microseconds
by cyphar 8y ago
I haven't tested this since then, but this wasn't the case in 2014[1]. I haven't noticed these issues since then, but back then it cost hundreds of microseconds -- 8 orders of magnitude more than "a few ns".
EDIT: I just re-did the relevant tests on Go 1.11.5. It's significantly better than in 2014 but it still costs between ~20us and ~50us ("only" 4 orders of magnitude more than "a few ns").
goos: linux
goarch: amd64
BenchmarkPut-8 50000 27502 ns/op
BenchmarkPutDefer-8 30000 46774 ns/op
BenchmarkGet-8 50000 29812 ns/op
BenchmarkGetDefer-8 20000 89701 ns/op
PASS
[1]: https://lk4d4.darth.io/posts/defer/ https://lk4d4.darth.io/posts/defer/
- yalue 8y agoIt actually did get significantly better in Go 1.8, in early 2017. See https://golang.org/doc/go1.8 https://golang.org/doc/go1.8 ("The overhead of deferred function calls has been reduced by about half."). However, I think this change was mostly just an optimization for common cases with few arguments or no closures (e.g. "defer f.Close()").
- cyphar 8y agoI just re-did the benchmarks with Go 1.11.5, it is much better but it's still 4 orders of magnitude more than "a few nanoseconds".