Skip to content

Commit

Permalink
all: use sub-benchmarks
Browse files Browse the repository at this point in the history
Updates #30
  • Loading branch information
ALTree committed Oct 21, 2016
1 parent 9c630da commit ae0f20d
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 78 deletions.
23 changes: 11 additions & 12 deletions exp_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package bigfloat_test

import (
"fmt"
"math"
"math/big"
"math/rand"
Expand Down Expand Up @@ -103,19 +104,17 @@ func TestExpSpecialValues(t *testing.T) {

// ---------- Benchmarks ----------

func benchmarkExp(z64 float64, prec uint, b *testing.B) {
z := big.NewFloat(z64).SetPrec(prec)
func BenchmarkExp(b *testing.B) {
z := big.NewFloat(2).SetPrec(1e5)
_ = bigfloat.Exp(z) // fill pi cache before benchmarking

b.ReportAllocs()
b.ResetTimer()
for n := 0; n < b.N; n++ {
bigfloat.Exp(z)
for _, prec := range []uint{1e2, 1e3, 1e4, 1e5} {
z = big.NewFloat(2).SetPrec(prec)
b.Run(fmt.Sprintf("%v", prec), func(b *testing.B) {
b.ReportAllocs()
for n := 0; n < b.N; n++ {
bigfloat.Exp(z)
}
})
}
}

func BenchmarkExp2Prec53(b *testing.B) { benchmarkExp(2, 53, b) }
func BenchmarkExp2Prec100(b *testing.B) { benchmarkExp(2, 1e2, b) }
func BenchmarkExp2Prec1000(b *testing.B) { benchmarkExp(2, 1e3, b) }
func BenchmarkExp2Prec10000(b *testing.B) { benchmarkExp(2, 1e4, b) }
func BenchmarkExp2Prec100000(b *testing.B) { benchmarkExp(2, 1e5, b) }
23 changes: 11 additions & 12 deletions log_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package bigfloat_test

import (
"fmt"
"math"
"math/big"
"math/rand"
Expand Down Expand Up @@ -97,19 +98,17 @@ func TestLogSpecialValues(t *testing.T) {

// ---------- Benchmarks ----------

func benchmarkLog(z64 float64, prec uint, b *testing.B) {
z := big.NewFloat(z64).SetPrec(prec)
func BenchmarkLog(b *testing.B) {
z := big.NewFloat(2).SetPrec(1e5)
_ = bigfloat.Log(z) // fill pi cache before benchmarking

b.ReportAllocs()
b.ResetTimer()
for n := 0; n < b.N; n++ {
bigfloat.Log(z)
for _, prec := range []uint{1e2, 1e3, 1e4, 1e5} {
z = big.NewFloat(2).SetPrec(prec)
b.Run(fmt.Sprintf("%v", prec), func(b *testing.B) {
b.ReportAllocs()
for n := 0; n < b.N; n++ {
bigfloat.Log(z)
}
})
}
}

func BenchmarkLog2Prec53(b *testing.B) { benchmarkLog(2, 53, b) }
func BenchmarkLog2Prec100(b *testing.B) { benchmarkLog(2, 1e2, b) }
func BenchmarkLog2Prec1000(b *testing.B) { benchmarkLog(2, 1e3, b) }
func BenchmarkLog2Prec10000(b *testing.B) { benchmarkLog(2, 1e4, b) }
func BenchmarkLog2Prec100000(b *testing.B) { benchmarkLog(2, 1e5, b) }
42 changes: 19 additions & 23 deletions misc_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package bigfloat

import (
"fmt"
"math/big"
"testing"
)
Expand Down Expand Up @@ -57,32 +58,27 @@ func TestPi(t *testing.T) {

// ---------- Benchmarks ----------

func benchmarkAgm(xf, yf float64, prec uint, b *testing.B) {
b.ReportAllocs()
x := new(big.Float).SetPrec(prec).SetFloat64(xf)
y := new(big.Float).SetPrec(prec).SetFloat64(yf)
for n := 0; n < b.N; n++ {
agm(x, y)
func BenchmarkAgm(b *testing.B) {
for _, prec := range []uint{1e2, 1e3, 1e4, 1e5} {
x := new(big.Float).SetPrec(prec).SetFloat64(1)
y := new(big.Float).SetPrec(prec).SetFloat64(0.125)
b.Run(fmt.Sprintf("%v", prec), func(b *testing.B) {
b.ReportAllocs()
for n := 0; n < b.N; n++ {
agm(x, y)
}
})
}
}

func BenchmarkAgmPrec53(b *testing.B) { benchmarkAgm(1, 0.125, 53, b) }
func BenchmarkAgmPrec100(b *testing.B) { benchmarkAgm(1, 0.125, 1e2, b) }
func BenchmarkAgmPrec1000(b *testing.B) { benchmarkAgm(1, 0.125, 1e3, b) }
func BenchmarkAgmPrec10000(b *testing.B) { benchmarkAgm(1, 0.125, 1e4, b) }
func BenchmarkAgmPrec100000(b *testing.B) { benchmarkAgm(1, 0.125, 1e5, b) }

func benchmarkPi(prec uint, b *testing.B) {
func BenchmarkPi(b *testing.B) {
enablePiCache = false
b.ReportAllocs()
for n := 0; n < b.N; n++ {
pi(prec)
for _, prec := range []uint{1e2, 1e3, 1e4, 1e5} {
b.Run(fmt.Sprintf("%v", prec), func(b *testing.B) {
b.ReportAllocs()
for n := 0; n < b.N; n++ {
pi(prec)
}
})
}
enablePiCache = true
}

func BenchmarkPiPrec53(b *testing.B) { benchmarkPi(53, b) }
func BenchmarkPiPrec100(b *testing.B) { benchmarkPi(1e2, b) }
func BenchmarkPiPrec1000(b *testing.B) { benchmarkPi(1e3, b) }
func BenchmarkPiPrec10000(b *testing.B) { benchmarkPi(1e4, b) }
func BenchmarkPiPrec100000(b *testing.B) { benchmarkPi(1e5, b) }
51 changes: 31 additions & 20 deletions pow_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package bigfloat_test

import (
"fmt"
"math"
"math/big"
"math/rand"
Expand Down Expand Up @@ -132,26 +133,36 @@ func TestPowSpecialValues(t *testing.T) {

// ---------- Benchmarks ----------

func benchmarkPow(z64, w64 float64, prec uint, b *testing.B) {
z := big.NewFloat(z64).SetPrec(prec)
w := big.NewFloat(w64).SetPrec(prec)
_ = bigfloat.Exp(z) // fill pi cache before benchmarking

b.ReportAllocs()
b.ResetTimer()
for n := 0; n < b.N; n++ {
bigfloat.Pow(z, w)
func BenchmarkPowInt(b *testing.B) {
z := big.NewFloat(2).SetPrec(1e5)
w := big.NewFloat(50).SetPrec(1e5)
_ = bigfloat.Pow(z, w) // fill pi cache before benchmarking

for _, prec := range []uint{1e2, 1e3, 1e4, 1e5} {
z = big.NewFloat(2).SetPrec(prec)
w = big.NewFloat(50).SetPrec(prec)
b.Run(fmt.Sprintf("%v", prec), func(b *testing.B) {
b.ReportAllocs()
for n := 0; n < b.N; n++ {
bigfloat.Pow(z, w)
}
})
}
}

func BenchmarkPowIntPrec53(b *testing.B) { benchmarkPow(2, 50, 53, b) }
func BenchmarkPowIntPrec100(b *testing.B) { benchmarkPow(2, 50, 1e2, b) }
func BenchmarkPowIntPrec1000(b *testing.B) { benchmarkPow(2, 50, 1e3, b) }
func BenchmarkPowIntPrec10000(b *testing.B) { benchmarkPow(2, 50, 1e4, b) }
func BenchmarkPowIntPrec100000(b *testing.B) { benchmarkPow(2, 50, 1e5, b) }

func BenchmarkPowPrec53(b *testing.B) { benchmarkPow(1.5, 1.5, 53, b) }
func BenchmarkPowPrec100(b *testing.B) { benchmarkPow(1.5, 1.5, 1e2, b) }
func BenchmarkPowPrec1000(b *testing.B) { benchmarkPow(1.5, 1.5, 1e3, b) }
func BenchmarkPowPrec10000(b *testing.B) { benchmarkPow(1.5, 1.5, 1e4, b) }
func BenchmarkPowPrec100000(b *testing.B) { benchmarkPow(1.5, 1.5, 1e5, b) }
func BenchmarkPow(b *testing.B) {
z := big.NewFloat(2).SetPrec(1e5)
w := big.NewFloat(1.5).SetPrec(1e5)
_ = bigfloat.Pow(z, w) // fill pi cache before benchmarking

for _, prec := range []uint{1e2, 1e3, 1e4, 1e5} {
z = big.NewFloat(2).SetPrec(prec)
w = big.NewFloat(1.5).SetPrec(prec)
b.Run(fmt.Sprintf("%v", prec), func(b *testing.B) {
b.ReportAllocs()
for n := 0; n < b.N; n++ {
bigfloat.Pow(z, w)
}
})
}
}
21 changes: 10 additions & 11 deletions sqrt_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package bigfloat_test

import (
"fmt"
"math"
"math/big"
"math/rand"
Expand Down Expand Up @@ -116,16 +117,14 @@ func TestSqrtSpecialValues(t *testing.T) {

// ---------- Benchmarks ----------

func benchmarkSqrt(z64 float64, prec uint, b *testing.B) {
b.ReportAllocs()
z := big.NewFloat(z64).SetPrec(prec)
for n := 0; n < b.N; n++ {
bigfloat.Sqrt(z)
func BenchmarkSqrt(b *testing.B) {
for _, prec := range []uint{1e2, 1e3, 1e4, 1e5} {
z := big.NewFloat(2).SetPrec(prec)
b.Run(fmt.Sprintf("%v", prec), func(b *testing.B) {
b.ReportAllocs()
for n := 0; n < b.N; n++ {
bigfloat.Sqrt(z)
}
})
}
}

func BenchmarkSqrt2Prec53(b *testing.B) { benchmarkSqrt(2, 53, b) }
func BenchmarkSqrt2Prec100(b *testing.B) { benchmarkSqrt(2, 1e2, b) }
func BenchmarkSqrt2Prec1000(b *testing.B) { benchmarkSqrt(2, 1e3, b) }
func BenchmarkSqrt2Prec10000(b *testing.B) { benchmarkSqrt(2, 1e4, b) }
func BenchmarkSqrt2Prec100000(b *testing.B) { benchmarkSqrt(2, 1e5, b) }

0 comments on commit ae0f20d

Please sign in to comment.