I'm a Software Engineer with a focus on Backend development using Golang.
cornelk / go-benchmark Goto Github PK
View Code? Open in Web Editor NEWGolang benchmarks used for optimizing code
License: Apache License 2.0
Golang benchmarks used for optimizing code
License: Apache License 2.0
In https://github.com/cornelk/go-benchmark/blob/main/defer_test.go, the DeferNo closure may be inflined, but the Defer one can't now, because the current official Go compiler never inlines functions containing defer calls.
Though it is true that defer calls have extra costs.
//go:noinline
func checkDefer(b *testing.B, index, item int) {
defer checkItem(b, index, item)
}
//go:noinline
func checkDeferNo(b *testing.B, index, item int) {
checkItem(b, index, item)
}
func BenchmarkDefer(b *testing.B) {
m := generateIntSlice(b)
for n := 0; n < b.N; n++ {
for i := 0; i < len(m); i++ {
var item = m[i]
checkDefer(b, i, item)
}
}
}
func BenchmarkDeferNo(b *testing.B) {
m := generateIntSlice(b)
for n := 0; n < b.N; n++ {
for i := 0; i < len(m); i++ {
var item = m[i]
checkDeferNo(b, i, item)
}
}
}
goos: linux
goarch: amd64
cpu: Intel(R) Core(TM) i5-4210U CPU @ 1.70GHz
BenchmarkDefer-4 2314526 515.7 ns/op
BenchmarkDeferNo-4 5631787 213.8 ns/op
The BCE optimization is always made for for-range loops.
func BenchmarkSliceReadForward(b *testing.B) {
m := generateIntSlice(b)
for n := 0; n < b.N; n++ {
for i := 0; i < len(m); i++ {
e := m[i]
checkItem(b, i, e)
}
}
}
func BenchmarkSliceReadBackwards(b *testing.B) {
m := generateIntSlice(b)
for n := 0; n < b.N; n++ {
for i := len(m) - 1; i >= 0; i-- {
e := m[i]
checkItem(b, i, e)
}
}
}
Having examples in the README under each benchmark would make it a lot easier to see what each benchmark does.
Example:
Using defer vs not using it
BenchmarkDefer-8 20000 58155 ns/op
BenchmarkDeferNo-8 300000 5378 ns/op
Defer
defer checkItem(b, i, item)
No defer
checkItem(b, i, item)
This approach might make it a bit more messy, so another solution could be to at least provide the links to each file used in the benchmarks for easier navigation.
Anyways, great job providing this! I think people will get much use of these benchmarks
$ gotv . test -bench=. interface_test.go helpers_test.go
[Run]: $HOME/.cache/gotv/tag_go1.21rc3/bin/go test -bench=. interface_test.go helpers_test.go
goos: linux
goarch: amd64
cpu: Intel(R) Core(TM) i5-4210U CPU @ 1.70GHz
BenchmarkValueUnsafePointer-4 27320748 43.15 ns/op
BenchmarkValueInterface-4 3027616 394.2 ns/op
BenchmarkReflect-4 2023377 588.6 ns/op
BenchmarkCast-4 3398929 357.0 ns/op
FYI.. github.com/pierrec/xxHash/xxHash32
is not in vendor
If we want to benchmark passing slices vs pointers-to-slices, we should change the functions like so:
- func getSliceItem(index int, m [BenchMarkSize]int) int
+ func getSliceItem(index int, m []int) int
- func getSlicePointerItem(index int, m *[BenchMarkSize]int) int
+ func getSlicePointerItem(index int, m *[]int) int
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.