gitee.com/quant1x/pkg@v0.2.8/fastjson/fastfloat/parse_timing_test.go (about)

     1  package fastfloat
     2  
     3  import (
     4  	"fmt"
     5  	"strconv"
     6  	"sync/atomic"
     7  	"testing"
     8  )
     9  
    10  func BenchmarkParseUint64(b *testing.B) {
    11  	for _, s := range []string{"0", "12", "12345", "1234567890", "9223372036854775807"} {
    12  		b.Run(s, func(b *testing.B) {
    13  			benchmarkParseUint64(b, s)
    14  		})
    15  	}
    16  }
    17  
    18  func BenchmarkParseUint64BestEffort(b *testing.B) {
    19  	for _, s := range []string{"0", "12", "12345", "1234567890", "9223372036854775807"} {
    20  		b.Run(s, func(b *testing.B) {
    21  			benchmarkParseUint64BestEffort(b, s)
    22  		})
    23  	}
    24  }
    25  
    26  func BenchmarkParseInt64(b *testing.B) {
    27  	for _, s := range []string{"0", "12", "12345", "1234567890", "9223372036854775807"} {
    28  		b.Run(s, func(b *testing.B) {
    29  			benchmarkParseInt64(b, s)
    30  		})
    31  	}
    32  }
    33  
    34  func BenchmarkParseInt64BestEffort(b *testing.B) {
    35  	for _, s := range []string{"0", "12", "12345", "1234567890", "9223372036854775807"} {
    36  		b.Run(s, func(b *testing.B) {
    37  			benchmarkParseInt64BestEffort(b, s)
    38  		})
    39  	}
    40  }
    41  
    42  func BenchmarkParseBestEffort(b *testing.B) {
    43  	for _, s := range []string{"0", "12", "12345", "1234567890", "1234.45678", "1234e45", "12.34e-34", "12345.1234567890", "12345.12345678901"} {
    44  		b.Run(s, func(b *testing.B) {
    45  			benchmarkParseBestEffort(b, s)
    46  		})
    47  	}
    48  }
    49  
    50  func BenchmarkParse(b *testing.B) {
    51  	for _, s := range []string{"0", "12", "12345", "1234567890", "1234.45678", "1234e45", "12.34e-34", "12345.1234567890", "12345.12345678901"} {
    52  		b.Run(s, func(b *testing.B) {
    53  			benchmarkParse(b, s)
    54  		})
    55  	}
    56  }
    57  
    58  func benchmarkParseUint64(b *testing.B, s string) {
    59  	b.Run("std", func(b *testing.B) {
    60  		b.ReportAllocs()
    61  		b.SetBytes(int64(len(s)))
    62  		b.RunParallel(func(pb *testing.PB) {
    63  			var d uint64
    64  			for pb.Next() {
    65  				dd, err := strconv.ParseUint(s, 10, 64)
    66  				if err != nil {
    67  					panic(fmt.Errorf("unexpected error: %s", err))
    68  				}
    69  				d += dd
    70  			}
    71  			atomic.AddUint64(&Sink, d)
    72  		})
    73  	})
    74  	b.Run("custom", func(b *testing.B) {
    75  		b.ReportAllocs()
    76  		b.SetBytes(int64(len(s)))
    77  		b.RunParallel(func(pb *testing.PB) {
    78  			var d uint64
    79  			for pb.Next() {
    80  				dd, err := ParseUint64(s)
    81  				if err != nil {
    82  					panic(fmt.Errorf("unexpected error: %s", err))
    83  				}
    84  				d += dd
    85  			}
    86  			atomic.AddUint64(&Sink, d)
    87  		})
    88  	})
    89  }
    90  
    91  func benchmarkParseInt64(b *testing.B, s string) {
    92  	b.Run("std", func(b *testing.B) {
    93  		b.ReportAllocs()
    94  		b.SetBytes(int64(len(s)))
    95  		b.RunParallel(func(pb *testing.PB) {
    96  			var d int64
    97  			for pb.Next() {
    98  				dd, err := strconv.ParseInt(s, 10, 64)
    99  				if err != nil {
   100  					panic(fmt.Errorf("unexpected error: %s", err))
   101  				}
   102  				d += dd
   103  			}
   104  			atomic.AddUint64(&Sink, uint64(d))
   105  		})
   106  	})
   107  	b.Run("custom", func(b *testing.B) {
   108  		b.ReportAllocs()
   109  		b.SetBytes(int64(len(s)))
   110  		b.RunParallel(func(pb *testing.PB) {
   111  			var d int64
   112  			for pb.Next() {
   113  				dd, err := ParseInt64(s)
   114  				if err != nil {
   115  					panic(fmt.Errorf("unexpected error: %s", err))
   116  				}
   117  				d += dd
   118  			}
   119  			atomic.AddUint64(&Sink, uint64(d))
   120  		})
   121  	})
   122  }
   123  
   124  func benchmarkParseUint64BestEffort(b *testing.B, s string) {
   125  	b.Run("std", func(b *testing.B) {
   126  		b.ReportAllocs()
   127  		b.SetBytes(int64(len(s)))
   128  		b.RunParallel(func(pb *testing.PB) {
   129  			var d uint64
   130  			for pb.Next() {
   131  				dd, err := strconv.ParseUint(s, 10, 64)
   132  				if err != nil {
   133  					panic(fmt.Errorf("unexpected error: %s", err))
   134  				}
   135  				d += dd
   136  			}
   137  			atomic.AddUint64(&Sink, d)
   138  		})
   139  	})
   140  	b.Run("custom", func(b *testing.B) {
   141  		b.ReportAllocs()
   142  		b.SetBytes(int64(len(s)))
   143  		b.RunParallel(func(pb *testing.PB) {
   144  			var d uint64
   145  			for pb.Next() {
   146  				d += ParseUint64BestEffort(s)
   147  			}
   148  			atomic.AddUint64(&Sink, d)
   149  		})
   150  	})
   151  }
   152  
   153  func benchmarkParseInt64BestEffort(b *testing.B, s string) {
   154  	b.Run("std", func(b *testing.B) {
   155  		b.ReportAllocs()
   156  		b.SetBytes(int64(len(s)))
   157  		b.RunParallel(func(pb *testing.PB) {
   158  			var d int64
   159  			for pb.Next() {
   160  				dd, err := strconv.ParseInt(s, 10, 64)
   161  				if err != nil {
   162  					panic(fmt.Errorf("unexpected error: %s", err))
   163  				}
   164  				d += dd
   165  			}
   166  			atomic.AddUint64(&Sink, uint64(d))
   167  		})
   168  	})
   169  	b.Run("custom", func(b *testing.B) {
   170  		b.ReportAllocs()
   171  		b.SetBytes(int64(len(s)))
   172  		b.RunParallel(func(pb *testing.PB) {
   173  			var d int64
   174  			for pb.Next() {
   175  				d += ParseInt64BestEffort(s)
   176  			}
   177  			atomic.AddUint64(&Sink, uint64(d))
   178  		})
   179  	})
   180  }
   181  
   182  func benchmarkParse(b *testing.B, s string) {
   183  	b.Run("std", func(b *testing.B) {
   184  		b.ReportAllocs()
   185  		b.SetBytes(int64(len(s)))
   186  		b.RunParallel(func(pb *testing.PB) {
   187  			var f float64
   188  			for pb.Next() {
   189  				ff, err := strconv.ParseFloat(s, 64)
   190  				if err != nil {
   191  					panic(fmt.Errorf("unexpected error: %s", err))
   192  				}
   193  				f += ff
   194  			}
   195  			atomic.AddUint64(&Sink, uint64(f))
   196  		})
   197  	})
   198  	b.Run("custom", func(b *testing.B) {
   199  		b.ReportAllocs()
   200  		b.SetBytes(int64(len(s)))
   201  		b.RunParallel(func(pb *testing.PB) {
   202  			var f float64
   203  			for pb.Next() {
   204  				ff, err := Parse(s)
   205  				if err != nil {
   206  					panic(fmt.Errorf("unexpected error in Parse(%q): %s", s, err))
   207  				}
   208  				f += ff
   209  			}
   210  			atomic.AddUint64(&Sink, uint64(f))
   211  		})
   212  	})
   213  }
   214  
   215  func benchmarkParseBestEffort(b *testing.B, s string) {
   216  	b.Run("std", func(b *testing.B) {
   217  		b.ReportAllocs()
   218  		b.SetBytes(int64(len(s)))
   219  		b.RunParallel(func(pb *testing.PB) {
   220  			var f float64
   221  			for pb.Next() {
   222  				ff, err := strconv.ParseFloat(s, 64)
   223  				if err != nil {
   224  					panic(fmt.Errorf("unexpected error: %s", err))
   225  				}
   226  				f += ff
   227  			}
   228  			atomic.AddUint64(&Sink, uint64(f))
   229  		})
   230  	})
   231  	b.Run("custom", func(b *testing.B) {
   232  		b.ReportAllocs()
   233  		b.SetBytes(int64(len(s)))
   234  		b.RunParallel(func(pb *testing.PB) {
   235  			var f float64
   236  			for pb.Next() {
   237  				f += ParseBestEffort(s)
   238  			}
   239  			atomic.AddUint64(&Sink, uint64(f))
   240  		})
   241  	})
   242  }
   243  
   244  var Sink uint64