Commit 0f5697a8 authored by Marcel van Lohuizen's avatar Marcel van Lohuizen

strconv: use Run for some benchmarks

This serves as an example of table-driven benchmarks which are analoguous to the common pattern for table-driven tests.

Change-Id: I47f94c121a7117dd1e4ba03b3f2f8bcb5da38063
Reviewed-on: https://go-review.googlesource.com/23470
Run-TryBot: Marcel van Lohuizen <mpvl@golang.org>
Reviewed-by: default avatarRobert Griesemer <gri@golang.org>
parent 03abde49
...@@ -183,59 +183,50 @@ func TestFtoaRandom(t *testing.T) { ...@@ -183,59 +183,50 @@ func TestFtoaRandom(t *testing.T) {
} }
} }
func BenchmarkFormatFloatDecimal(b *testing.B) { var ftoaBenches = []struct {
for i := 0; i < b.N; i++ { name string
FormatFloat(33909, 'g', -1, 64) float float64
} fmt byte
prec int
bitSize int
}{
{"Decimal", 33909, 'g', -1, 64},
{"Float", 339.7784, 'g', -1, 64},
{"Exp", -5.09e75, 'g', -1, 64},
{"NegExp", -5.11e-95, 'g', -1, 64},
{"Big", 123456789123456789123456789, 'g', -1, 64},
{"BinaryExp", -1, 'b', -1, 64},
{"32Integer", 33909, 'g', -1, 32},
{"32ExactFraction", 3.375, 'g', -1, 32},
{"32Point", 339.7784, 'g', -1, 32},
{"32Exp", -5.09e25, 'g', -1, 32},
{"32NegExp", -5.11e-25, 'g', -1, 32},
{"64Fixed1", 123456, 'e', 3, 64},
{"64Fixed2", 123.456, 'e', 3, 64},
{"64Fixed3", 1.23456e+78, 'e', 3, 64},
{"64Fixed4", 1.23456e-78, 'e', 3, 64},
} }
func BenchmarkFormatFloat(b *testing.B) { func BenchmarkFormatFloat(b *testing.B) {
for _, c := range ftoaBenches {
b.Run(c.name, func(b *testing.B) {
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
FormatFloat(339.7784, 'g', -1, 64) FormatFloat(c.float, c.fmt, c.prec, c.bitSize)
}
}
func BenchmarkFormatFloatExp(b *testing.B) {
for i := 0; i < b.N; i++ {
FormatFloat(-5.09e75, 'g', -1, 64)
}
}
func BenchmarkFormatFloatNegExp(b *testing.B) {
for i := 0; i < b.N; i++ {
FormatFloat(-5.11e-95, 'g', -1, 64)
} }
} })
func BenchmarkFormatFloatBig(b *testing.B) {
for i := 0; i < b.N; i++ {
FormatFloat(123456789123456789123456789, 'g', -1, 64)
} }
} }
func benchmarkAppendFloat(b *testing.B, f float64, fmt byte, prec, bitSize int) { func BenchmarkAppendFloat(b *testing.B) {
dst := make([]byte, 30) dst := make([]byte, 30)
for _, c := range ftoaBenches {
b.Run(c.name, func(b *testing.B) {
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
AppendFloat(dst[:0], f, fmt, prec, bitSize) AppendFloat(dst[:0], c.float, c.fmt, c.prec, c.bitSize)
}
})
} }
} }
func BenchmarkAppendFloatDecimal(b *testing.B) { benchmarkAppendFloat(b, 33909, 'g', -1, 64) }
func BenchmarkAppendFloat(b *testing.B) { benchmarkAppendFloat(b, 339.7784, 'g', -1, 64) }
func BenchmarkAppendFloatExp(b *testing.B) { benchmarkAppendFloat(b, -5.09e75, 'g', -1, 64) }
func BenchmarkAppendFloatNegExp(b *testing.B) { benchmarkAppendFloat(b, -5.11e-95, 'g', -1, 64) }
func BenchmarkAppendFloatBig(b *testing.B) {
benchmarkAppendFloat(b, 123456789123456789123456789, 'g', -1, 64)
}
func BenchmarkAppendFloatBinaryExp(b *testing.B) { benchmarkAppendFloat(b, -1, 'b', -1, 64) }
func BenchmarkAppendFloat32Integer(b *testing.B) { benchmarkAppendFloat(b, 33909, 'g', -1, 32) }
func BenchmarkAppendFloat32ExactFraction(b *testing.B) { benchmarkAppendFloat(b, 3.375, 'g', -1, 32) }
func BenchmarkAppendFloat32Point(b *testing.B) { benchmarkAppendFloat(b, 339.7784, 'g', -1, 32) }
func BenchmarkAppendFloat32Exp(b *testing.B) { benchmarkAppendFloat(b, -5.09e25, 'g', -1, 32) }
func BenchmarkAppendFloat32NegExp(b *testing.B) { benchmarkAppendFloat(b, -5.11e-25, 'g', -1, 32) }
func BenchmarkAppendFloat64Fixed1(b *testing.B) { benchmarkAppendFloat(b, 123456, 'e', 3, 64) }
func BenchmarkAppendFloat64Fixed2(b *testing.B) { benchmarkAppendFloat(b, 123.456, 'e', 3, 64) }
func BenchmarkAppendFloat64Fixed3(b *testing.B) { benchmarkAppendFloat(b, 1.23456e+78, 'e', 3, 64) }
func BenchmarkAppendFloat64Fixed4(b *testing.B) { benchmarkAppendFloat(b, 1.23456e-78, 'e', 3, 64) }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment