Skip to content

Instantly share code, notes, and snippets.

@eliben
Created June 22, 2023 12:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eliben/3ad700c3589d814eb87dfef704083abe to your computer and use it in GitHub Desktop.
Save eliben/3ad700c3589d814eb87dfef704083abe to your computer and use it in GitHub Desktop.
package assembly
import (
"runtime"
"testing"
)
var Sink int
//go:noinline
func getInputContents() []byte {
n := 400000
buf := make([]byte, n)
for i := 0; i < n; i++ {
buf[i] = byte(n % 32)
}
return buf
}
func countCond(b []byte) int {
result := 0
for i := 0; i < len(b); i++ {
if isCond(b[i]) {
result += 1
}
}
return result
}
func isCond(b byte) bool {
if b == 3 || b == 7 || b == 11 || b == 17 || b == 19 || b == 31 {
return true
}
return false
}
func doloopNoSink(n int) {
inp := getInputContents()
for i := 0; i < n; i++ {
countCond(inp)
}
}
func doloopYesSink(n int) {
inp := getInputContents()
for i := 0; i < n; i++ {
Sink += countCond(inp)
}
}
func BenchmarkNoSink(b *testing.B) {
inp := getInputContents()
b.ResetTimer()
for i := 0; i < b.N; i++ {
countCond(inp)
}
}
func BenchmarkYesSink(b *testing.B) {
inp := getInputContents()
b.ResetTimer()
for i := 0; i < b.N; i++ {
Sink += countCond(inp)
}
}
func BenchmarkKeepalive(b *testing.B) {
inp := getInputContents()
b.ResetTimer()
for i := 0; i < b.N; i++ {
runtime.KeepAlive(countCond(inp))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment