Skip to content

Instantly share code, notes, and snippets.

@kokes
Created July 20, 2020 20:55
Show Gist options
  • Save kokes/e94a9e0f60dbe6d439a35287a38e5c14 to your computer and use it in GitHub Desktop.
Save kokes/e94a9e0f60dbe6d439a35287a38e5c14 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"math"
"math/rand"
"time"
)
func main() {
n := 100 * 1000
mod := float64(math.Pi)
in := make([]float64, 0, n)
for j := 0; j < n; j++ {
in = append(in, rand.NormFloat64()) // ExpFloat64, Float64
}
out := make([]float64, n)
t := time.Now()
NaiveMod(in, out, mod)
fmt.Println("NaiveMod", time.Since(t))
out = make([]float64, n)
t = time.Now()
MathMod(in, out, mod)
fmt.Println("MathMod", time.Since(t))
}
func MathMod(in, out []float64, mod float64) {
for i := range in {
out[i] = math.Mod(in[i], mod)
}
}
func NaiveMod(in, out []float64, mod float64) {
for i := range in {
out[i] = in[i] - (float64(int64(in[i]/mod)) * mod)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment