]> Cypherpunks repositories - gostls13.git/commitdiff
strconv: use Run for some benchmarks
authorMarcel van Lohuizen <mpvl@golang.org>
Thu, 26 May 2016 10:10:28 +0000 (12:10 +0200)
committerMarcel van Lohuizen <mpvl@golang.org>
Thu, 2 Jun 2016 20:47:29 +0000 (20:47 +0000)
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>
src/strconv/ftoa_test.go

index 0b9f0feafa675aea7a3609385b92c452c7e0877e..1d25242ff3fee2fe7aeaf5006e7e76b6a52286ec 100644 (file)
@@ -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)
-       }
-}
-
-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)
+       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) }