Last active
August 29, 2015 14:09
-
-
Save zach-klippenstein/b29936061804481bcaa5 to your computer and use it in GitHub Desktop.
Float Comparison Benchmark: Subtraction vs Multiplication
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 main | |
import ( | |
"math" | |
"math/rand" | |
"runtime" | |
"testing" | |
) | |
const DELTA_MULT = 10000000000 | |
const DELTA_SUB = 0.00000001 | |
func init() { | |
runtime.GOMAXPROCS(1) | |
} | |
func compareMult(a float64, b float64, delta float64) bool { | |
return int64(a*delta) == int64(b*delta) | |
} | |
func compareSub(a float64, b float64, delta float64) bool { | |
return math.Abs(a-b) < delta | |
} | |
func BenchmarkMultiplication(b *testing.B) { | |
float1 := rand.Float64() | |
float2 := rand.Float64() | |
b.ResetTimer() | |
for i := 0; i < b.N; i++ { | |
compareMult(float1, float2, DELTA_MULT) | |
} | |
} | |
func BenchmarkSubtraction(b *testing.B) { | |
float1 := rand.Float64() | |
float2 := rand.Float64() | |
b.ResetTimer() | |
for i := 0; i < b.N; i++ { | |
compareSub(float1, float2, DELTA_SUB) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Results on a 2014 MacBook Pro with a 2.6 GHz Core i5: