diff options
Diffstat (limited to 'src/pkg/strconv/ftoa_test.go')
-rw-r--r-- | src/pkg/strconv/ftoa_test.go | 70 |
1 files changed, 27 insertions, 43 deletions
diff --git a/src/pkg/strconv/ftoa_test.go b/src/pkg/strconv/ftoa_test.go index 7d8617a85..39b861547 100644 --- a/src/pkg/strconv/ftoa_test.go +++ b/src/pkg/strconv/ftoa_test.go @@ -163,6 +163,7 @@ func TestFtoaRandom(t *testing.T) { for i := 0; i < N; i++ { bits := uint64(rand.Uint32())<<32 | uint64(rand.Uint32()) x := math.Float64frombits(bits) + shortFast := FormatFloat(x, 'g', -1, 64) SetOptimize(false) shortSlow := FormatFloat(x, 'g', -1, 64) @@ -170,23 +171,15 @@ func TestFtoaRandom(t *testing.T) { if shortSlow != shortFast { t.Errorf("%b printed as %s, want %s", x, shortFast, shortSlow) } - } -} -func TestAppendFloatDoesntAllocate(t *testing.T) { - n := numAllocations(func() { - var buf [64]byte - AppendFloat(buf[:0], 1.23, 'g', 5, 64) - }) - want := 1 // TODO(bradfitz): this might be 0, once escape analysis is better - if n != want { - t.Errorf("with local buffer, did %d allocations, want %d", n, want) - } - n = numAllocations(func() { - AppendFloat(globalBuf[:0], 1.23, 'g', 5, 64) - }) - if n != 0 { - t.Errorf("with reused buffer, did %d allocations, want 0", n) + prec := rand.Intn(12) + 5 + shortFast = FormatFloat(x, 'e', prec, 64) + SetOptimize(false) + shortSlow = FormatFloat(x, 'e', prec, 64) + SetOptimize(true) + if shortSlow != shortFast { + t.Errorf("%b printed as %s, want %s", x, shortFast, shortSlow) + } } } @@ -220,37 +213,28 @@ func BenchmarkFormatFloatBig(b *testing.B) { } } -func BenchmarkAppendFloatDecimal(b *testing.B) { - dst := make([]byte, 0, 30) +func benchmarkAppendFloat(b *testing.B, f float64, fmt byte, prec, bitSize int) { + dst := make([]byte, 30) for i := 0; i < b.N; i++ { - AppendFloat(dst, 33909, 'g', -1, 64) + AppendFloat(dst[:0], f, fmt, prec, bitSize) } } -func BenchmarkAppendFloat(b *testing.B) { - dst := make([]byte, 0, 30) - for i := 0; i < b.N; i++ { - AppendFloat(dst, 339.7784, 'g', -1, 64) - } -} - -func BenchmarkAppendFloatExp(b *testing.B) { - dst := make([]byte, 0, 30) - for i := 0; i < b.N; i++ { - AppendFloat(dst, -5.09e75, 'g', -1, 64) - } +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 BenchmarkAppendFloatNegExp(b *testing.B) { - dst := make([]byte, 0, 30) - for i := 0; i < b.N; i++ { - AppendFloat(dst, -5.11e-95, 'g', -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 BenchmarkAppendFloatBig(b *testing.B) { - dst := make([]byte, 0, 30) - for i := 0; i < b.N; i++ { - AppendFloat(dst, 123456789123456789123456789, 'g', -1, 64) - } -} +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) } |