Last active
August 25, 2019 16:27
-
-
Save knzm/773bb50004b8ebc619ffd57ef567fdc8 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 version | |
go version go1.12.5 darwin/amd64 | |
$ go test -bench . | |
goos: darwin | |
goarch: amd64 | |
BenchmarkContainsUint64-4 500000 2286 ns/op | |
BenchmarkContainsUint64Unroll2-4 1000000 1988 ns/op | |
BenchmarkContainsUint64Unroll4-4 1000000 1589 ns/op | |
BenchmarkContainsUint64Unroll8-4 1000000 1795 ns/op | |
PASS | |
$ go version | |
go version go1.13rc1 linux/amd64 | |
$ go test -bench . | |
goos: linux | |
goarch: amd64 | |
BenchmarkContainsUint64-2 596392 1937 ns/op | |
BenchmarkContainsUint64Unroll2-2 579490 1895 ns/op | |
BenchmarkContainsUint64Unroll4-2 700369 1567 ns/op | |
BenchmarkContainsUint64Unroll8-2 667084 2268 ns/op | |
PASS |
This file contains hidden or 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 unroll | |
func ContainsUint64(ids []uint64, id uint64) bool { | |
for i := 0; i < len(ids); i++ { | |
if ids[i] == id { | |
return true | |
} | |
} | |
return false | |
} | |
func ContainsUint64Unroll2(ids []uint64, id uint64) bool { | |
var i int | |
for i = len(ids) - 1; i >= 1; i -= 2 { | |
if ids[i] == id || ids[i-1] == id { | |
return true | |
} | |
} | |
for ; i >= 0; i-- { | |
if ids[i] == id { | |
return true | |
} | |
} | |
return false | |
} | |
func ContainsUint64Unroll4(ids []uint64, id uint64) bool { | |
var i int | |
for i = len(ids) - 1; i >= 3; i -= 4 { | |
if ids[i] == id || | |
ids[i-1] == id || | |
ids[i-2] == id || | |
ids[i-3] == id { | |
return true | |
} | |
} | |
for ; i >= 0; i-- { | |
if ids[i] == id { | |
return true | |
} | |
} | |
return false | |
} | |
func ContainsUint64Unroll8(ids []uint64, id uint64) bool { | |
var i int | |
for i = len(ids) - 1; i >= 7; i -= 8 { | |
if ids[i] == id || | |
ids[i-1] == id || | |
ids[i-2] == id || | |
ids[i-3] == id || | |
ids[i-4] == id || | |
ids[i-5] == id || | |
ids[i-6] == id || | |
ids[i-7] == id { | |
return true | |
} | |
} | |
for ; i >= 0; i-- { | |
if ids[i] == id { | |
return true | |
} | |
} | |
return false | |
} |
This file contains hidden or 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 unroll | |
import ( | |
"testing" | |
) | |
func makeBenchData(n int) []uint64 { | |
ids := make([]uint64, n) | |
for i := 0; i < n; i++ { | |
ids[i] = uint64(i) | |
} | |
return ids | |
} | |
func BenchmarkContainsUint64(b *testing.B) { | |
ids := makeBenchData(10000) | |
for i := 0; i < b.N; i++ { | |
ContainsUint64(ids, 5000) | |
} | |
} | |
func BenchmarkContainsUint64Unroll2(b *testing.B) { | |
ids := makeBenchData(10000) | |
for i := 0; i < b.N; i++ { | |
ContainsUint64Unroll2(ids, 5000) | |
} | |
} | |
func BenchmarkContainsUint64Unroll4(b *testing.B) { | |
ids := makeBenchData(10000) | |
for i := 0; i < b.N; i++ { | |
ContainsUint64Unroll4(ids, 5000) | |
} | |
} | |
func BenchmarkContainsUint64Unroll8(b *testing.B) { | |
ids := makeBenchData(10000) | |
for i := 0; i < b.N; i++ { | |
ContainsUint64Unroll8(ids, 5000) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment