From 0f5697a81deab54f1673a48bd0ce613ebf1ddae6 Mon Sep 17 00:00:00 2001 From: Marcel van Lohuizen Date: Thu, 26 May 2016 12:10:28 +0200 Subject: [PATCH] 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 Reviewed-by: Robert Griesemer --- src/strconv/ftoa_test.go | 85 ++++++++++++++++++---------------------- 1 file changed, 38 insertions(+), 47 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}, + + {"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) { - for i := 0; i < b.N; i++ { - FormatFloat(339.7784, 'g', -1, 64) + 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 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) - 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) }