Skip to content

Instantly share code, notes, and snippets.

@sapphi-red
Created September 3, 2020 14:11
Show Gist options
  • Save sapphi-red/2de1653580ca1c2918039a886acc0035 to your computer and use it in GitHub Desktop.
Save sapphi-red/2de1653580ca1c2918039a886acc0035 to your computer and use it in GitHub Desktop.
prediction fail
package prediction
import (
"math/rand"
"testing"
)
func BenchmarkEvery(b *testing.B) {
howmany := 10000000
out := [10000000]int{}
index := 0
b.ResetTimer()
for howmany != 0 {
out[index] = rand.Int()
index += 1
howmany--
}
}
func BenchmarkOnlyOddPredictionFail(b *testing.B) {
howmany := 10000000
out := [10000000]int{}
index := 0
b.ResetTimer()
for howmany != 0 {
val := rand.Int()
if val%2 != 0 {
out[index] = val
index += 1
}
howmany--
}
}
func BenchmarkOnlyOddNonPrediction(b *testing.B) {
howmany := 10000000
out := [10000000]int{}
index := 0
b.ResetTimer()
for howmany != 0 {
val := rand.Int()
out[index] = val
index += val & 1
howmany--
}
}
@sapphi-red
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment