170
171
if shortSlow != shortFast {
171
172
t.Errorf("%b printed as %s, want %s", x, shortFast, shortSlow)
176
func TestAppendFloatDoesntAllocate(t *testing.T) {
177
n := numAllocations(func() {
179
AppendFloat(buf[:0], 1.23, 'g', 5, 64)
181
want := 1 // TODO(bradfitz): this might be 0, once escape analysis is better
183
t.Errorf("with local buffer, did %d allocations, want %d", n, want)
185
n = numAllocations(func() {
186
AppendFloat(globalBuf[:0], 1.23, 'g', 5, 64)
189
t.Errorf("with reused buffer, did %d allocations, want 0", n)
175
prec := rand.Intn(12) + 5
176
shortFast = FormatFloat(x, 'e', prec, 64)
178
shortSlow = FormatFloat(x, 'e', prec, 64)
180
if shortSlow != shortFast {
181
t.Errorf("%b printed as %s, want %s", x, shortFast, shortSlow)
223
func BenchmarkAppendFloatDecimal(b *testing.B) {
224
dst := make([]byte, 0, 30)
225
for i := 0; i < b.N; i++ {
226
AppendFloat(dst, 33909, 'g', -1, 64)
230
func BenchmarkAppendFloat(b *testing.B) {
231
dst := make([]byte, 0, 30)
232
for i := 0; i < b.N; i++ {
233
AppendFloat(dst, 339.7784, 'g', -1, 64)
237
func BenchmarkAppendFloatExp(b *testing.B) {
238
dst := make([]byte, 0, 30)
239
for i := 0; i < b.N; i++ {
240
AppendFloat(dst, -5.09e75, 'g', -1, 64)
244
func BenchmarkAppendFloatNegExp(b *testing.B) {
245
dst := make([]byte, 0, 30)
246
for i := 0; i < b.N; i++ {
247
AppendFloat(dst, -5.11e-95, 'g', -1, 64)
216
func benchmarkAppendFloat(b *testing.B, f float64, fmt byte, prec, bitSize int) {
217
dst := make([]byte, 30)
218
for i := 0; i < b.N; i++ {
219
AppendFloat(dst[:0], f, fmt, prec, bitSize)
223
func BenchmarkAppendFloatDecimal(b *testing.B) { benchmarkAppendFloat(b, 33909, 'g', -1, 64) }
224
func BenchmarkAppendFloat(b *testing.B) { benchmarkAppendFloat(b, 339.7784, 'g', -1, 64) }
225
func BenchmarkAppendFloatExp(b *testing.B) { benchmarkAppendFloat(b, -5.09e75, 'g', -1, 64) }
226
func BenchmarkAppendFloatNegExp(b *testing.B) { benchmarkAppendFloat(b, -5.11e-95, 'g', -1, 64) }
251
227
func BenchmarkAppendFloatBig(b *testing.B) {
252
dst := make([]byte, 0, 30)
253
for i := 0; i < b.N; i++ {
254
AppendFloat(dst, 123456789123456789123456789, 'g', -1, 64)
228
benchmarkAppendFloat(b, 123456789123456789123456789, 'g', -1, 64)
231
func BenchmarkAppendFloat32Integer(b *testing.B) { benchmarkAppendFloat(b, 33909, 'g', -1, 32) }
232
func BenchmarkAppendFloat32ExactFraction(b *testing.B) { benchmarkAppendFloat(b, 3.375, 'g', -1, 32) }
233
func BenchmarkAppendFloat32Point(b *testing.B) { benchmarkAppendFloat(b, 339.7784, 'g', -1, 32) }
234
func BenchmarkAppendFloat32Exp(b *testing.B) { benchmarkAppendFloat(b, -5.09e25, 'g', -1, 32) }
235
func BenchmarkAppendFloat32NegExp(b *testing.B) { benchmarkAppendFloat(b, -5.11e-25, 'g', -1, 32) }
237
func BenchmarkAppendFloat64Fixed1(b *testing.B) { benchmarkAppendFloat(b, 123456, 'e', 3, 64) }
238
func BenchmarkAppendFloat64Fixed2(b *testing.B) { benchmarkAppendFloat(b, 123.456, 'e', 3, 64) }
239
func BenchmarkAppendFloat64Fixed3(b *testing.B) { benchmarkAppendFloat(b, 1.23456e+78, 'e', 3, 64) }
240
func BenchmarkAppendFloat64Fixed4(b *testing.B) { benchmarkAppendFloat(b, 1.23456e-78, 'e', 3, 64) }