gitee.com/quant1x/gox@v1.7.6/fastjson/fastfloat_timing_test.go (about) 1 package fastjson 2 3 import ( 4 "fmt" 5 "strconv" 6 "sync/atomic" 7 "testing" 8 ) 9 10 func BenchmarkParseInt64BestEffort(b *testing.B) { 11 for _, s := range []string{"0", "12", "12345", "1234567890", "9223372036854775807"} { 12 b.Run(s, func(b *testing.B) { 13 benchmarkParseInt64BestEffort(b, s) 14 }) 15 } 16 } 17 18 func BenchmarkParseBestEffort(b *testing.B) { 19 for _, s := range []string{"0", "12", "12345", "1234567890", "1234.45678", "1234e45", "12.34e-34"} { 20 b.Run(s, func(b *testing.B) { 21 benchmarkParseBestEffort(b, s) 22 }) 23 } 24 } 25 26 func benchmarkParseInt64BestEffort(b *testing.B, s string) { 27 b.Run("std", func(b *testing.B) { 28 b.ReportAllocs() 29 b.SetBytes(int64(len(s))) 30 b.RunParallel(func(pb *testing.PB) { 31 var d int64 32 for pb.Next() { 33 dd, err := strconv.ParseInt(s, 10, 64) 34 if err != nil { 35 panic(fmt.Errorf("unexpected error: %s", err)) 36 } 37 d += dd 38 } 39 atomic.AddUint64(&Sink, uint64(d)) 40 }) 41 }) 42 b.Run("custom", func(b *testing.B) { 43 b.ReportAllocs() 44 b.SetBytes(int64(len(s))) 45 b.RunParallel(func(pb *testing.PB) { 46 var d int64 47 for pb.Next() { 48 d += ParseInt64BestEffort(s) 49 } 50 atomic.AddUint64(&Sink, uint64(d)) 51 }) 52 }) 53 } 54 55 func benchmarkParseBestEffort(b *testing.B, s string) { 56 b.Run("std", func(b *testing.B) { 57 b.ReportAllocs() 58 b.SetBytes(int64(len(s))) 59 b.RunParallel(func(pb *testing.PB) { 60 var f float64 61 for pb.Next() { 62 ff, err := strconv.ParseFloat(s, 64) 63 if err != nil { 64 panic(fmt.Errorf("unexpected error: %s", err)) 65 } 66 f += ff 67 } 68 atomic.AddUint64(&Sink, uint64(f)) 69 }) 70 }) 71 b.Run("custom", func(b *testing.B) { 72 b.ReportAllocs() 73 b.SetBytes(int64(len(s))) 74 b.RunParallel(func(pb *testing.PB) { 75 var f float64 76 for pb.Next() { 77 f += ParseBestEffort(s) 78 } 79 atomic.AddUint64(&Sink, uint64(f)) 80 }) 81 }) 82 } 83 84 var Sink uint64