Created
November 11, 2024 14:49
-
-
Save florianl/61de8e1434a513a1beb3f1f76c31cf08 to your computer and use it in GitHub Desktop.
modulo benchmarking for https://github.com/elastic/go-freelru/pull/60
This file contains 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 modulo | |
func fastModulo(x, n uint32) uint32 { | |
return uint32((uint64(x) * uint64(n)) >> 32) | |
} | |
func regularModulo(x, n uint32) uint32 { | |
return x % n | |
} |
This file contains 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 modulo | |
import ( | |
"fmt" | |
"testing" | |
) | |
func BenchmarkModulo(b *testing.B) { | |
testCases := []struct { | |
name string | |
fn func(uint32, uint32) uint32 | |
}{ | |
{"FastModulo", fastModulo}, | |
{"RegularModulo", regularModulo}, | |
} | |
values := []struct { | |
x, n uint32 | |
}{ | |
{x: 3, n: 4095}, | |
{x: 4095, n: 3}, | |
{x: 65535, n: 19}, | |
{x: 19, n: 65535}, | |
{x: 65535 - 1, n: 19}, | |
{x: 19, n: 65535 - 1}, | |
{x: 4_294_967_295 - 1, n: 65535}, | |
{x: 4_294_967_295 - 1, n: 65535 - 1}, | |
} | |
for _, tc := range testCases { | |
for _, value := range values { | |
b.Run(tc.name+fmt.Sprintf("_%d over %d", value.x, value.n), func(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
_ = tc.fn(value.x, value.n) | |
} | |
}) | |
} | |
} | |
} |
This file contains 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
$ go test -bench=. -benchmem | |
goos: linux | |
goarch: amd64 | |
pkg: xch.gl/modulo | |
cpu: 12th Gen Intel(R) Core(TM) i7-12700H | |
BenchmarkModulo/FastModulo_3_over_4095-20 1000000000 1.141 ns/op 0 B/op 0 allocs/op | |
BenchmarkModulo/FastModulo_4095_over_3-20 1000000000 1.138 ns/op 0 B/op 0 allocs/op | |
BenchmarkModulo/FastModulo_65535_over_19-20 1000000000 1.137 ns/op 0 B/op 0 allocs/op | |
BenchmarkModulo/FastModulo_19_over_65535-20 1000000000 1.134 ns/op 0 B/op 0 allocs/op | |
BenchmarkModulo/FastModulo_65534_over_19-20 1000000000 1.136 ns/op 0 B/op 0 allocs/op | |
BenchmarkModulo/FastModulo_19_over_65534-20 1000000000 1.150 ns/op 0 B/op 0 allocs/op | |
BenchmarkModulo/FastModulo_4294967294_over_65535-20 1000000000 1.161 ns/op 0 B/op 0 allocs/op | |
BenchmarkModulo/FastModulo_4294967294_over_65534-20 1000000000 1.166 ns/op 0 B/op 0 allocs/op | |
BenchmarkModulo/RegularModulo_3_over_4095-20 908406850 1.307 ns/op 0 B/op 0 allocs/op | |
BenchmarkModulo/RegularModulo_4095_over_3-20 914586667 1.298 ns/op 0 B/op 0 allocs/op | |
BenchmarkModulo/RegularModulo_65535_over_19-20 912191052 1.307 ns/op 0 B/op 0 allocs/op | |
BenchmarkModulo/RegularModulo_19_over_65535-20 917458092 1.298 ns/op 0 B/op 0 allocs/op | |
BenchmarkModulo/RegularModulo_65534_over_19-20 907173954 1.310 ns/op 0 B/op 0 allocs/op | |
BenchmarkModulo/RegularModulo_19_over_65534-20 915687758 1.294 ns/op 0 B/op 0 allocs/op | |
BenchmarkModulo/RegularModulo_4294967294_over_65535-20 927088612 1.310 ns/op 0 B/op 0 allocs/op | |
BenchmarkModulo/RegularModulo_4294967294_over_65534-20 914863524 1.289 ns/op 0 B/op 0 allocs/op | |
PASS | |
ok xch.gl/modulo 20.744s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment