-
-
Save eliben/3ad700c3589d814eb87dfef704083abe to your computer and use it in GitHub Desktop.
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 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