diff options
| author | Marcel van Lohuizen <mpvl@golang.org> | 2016-05-26 12:10:28 +0200 |
|---|---|---|
| committer | Marcel van Lohuizen <mpvl@golang.org> | 2016-06-02 20:47:29 +0000 |
| commit | 0f5697a81deab54f1673a48bd0ce613ebf1ddae6 (patch) | |
| tree | ce79d17f4adc000f918cc4bd0b0f3a86c717050b /src | |
| parent | 03abde49713b46366fa47a037040697c8fdad3bb (diff) | |
| download | go-0f5697a81deab54f1673a48bd0ce613ebf1ddae6.tar.xz | |
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: Robert Griesemer <gri@golang.org>
Diffstat (limited to 'src')
| -rw-r--r-- | src/strconv/ftoa_test.go | 81 |
1 files changed, 36 insertions, 45 deletions
diff --git a/src/strconv/ftoa_test.go b/src/strconv/ftoa_test.go index 0b9f0feafa..1d25242ff3 100644 --- a/src/strconv/ftoa_test.go +++ b/src/strconv/ftoa_test.go @@ -183,59 +183,50 @@ func TestFtoaRandom(t *testing.T) { } } -func BenchmarkFormatFloatDecimal(b *testing.B) { - for i := 0; i < b.N; i++ { - FormatFloat(33909, 'g', -1, 64) - } -} +var ftoaBenches = []struct { + name string + 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}, -func BenchmarkFormatFloat(b *testing.B) { - for i := 0; i < b.N; i++ { - FormatFloat(339.7784, 'g', -1, 64) - } -} + {"Big", 123456789123456789123456789, 'g', -1, 64}, + {"BinaryExp", -1, 'b', -1, 64}, -func BenchmarkFormatFloatExp(b *testing.B) { - for i := 0; i < b.N; i++ { - FormatFloat(-5.09e75, 'g', -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}, -func BenchmarkFormatFloatNegExp(b *testing.B) { - for i := 0; i < b.N; i++ { - FormatFloat(-5.11e-95, 'g', -1, 64) - } + {"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 BenchmarkFormatFloatBig(b *testing.B) { - for i := 0; i < b.N; i++ { - FormatFloat(123456789123456789123456789, 'g', -1, 64) +func BenchmarkFormatFloat(b *testing.B) { + for _, c := range ftoaBenches { + b.Run(c.name, func(b *testing.B) { + for i := 0; i < b.N; i++ { + FormatFloat(c.float, c.fmt, c.prec, c.bitSize) + } + }) } } -func benchmarkAppendFloat(b *testing.B, f float64, fmt byte, prec, bitSize int) { +func BenchmarkAppendFloat(b *testing.B) { dst := make([]byte, 30) - for i := 0; i < b.N; i++ { - AppendFloat(dst[:0], f, fmt, prec, bitSize) + for _, c := range ftoaBenches { + b.Run(c.name, func(b *testing.B) { + for i := 0; i < b.N; i++ { + 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) } |
