Skip to content

Latest commit

 

History

History
148 lines (132 loc) · 15.1 KB

README.md

File metadata and controls

148 lines (132 loc) · 15.1 KB

Benchmarks

This section provides benchmarks for the udecimal library in comparision with shopspring/decimal. Some benchmarks have fallback suffix which means the operation can't be done by using u128 and falls back to big.Int API.

For full benchmark details, checkout benchmark-udec.txt and benchstat.txt

Benchmark Results

NOTE: The results are for references and can be varied depending on the hardware

Environment

goos: linux
goarch: amd64
pkg: github.com/quagmt/udecimal/benchmarks
cpu: Intel(R) Core(TM) i9-14900HX

Results

# Arithmetic operations
BenchmarkAdd/1234567890123456789.1234567890123456879.Add(1111.1789)-32                     	100000000	 10.91 ns/op	   0 B/op	 0 allocs/op
BenchmarkAdd/548751.15465466546.Add(1542.456487)-32                                      	100000000	 11.44 ns/op	   0 B/op	 0 allocs/op
BenchmarkSub/1234567890123456789.1234567890123456879.Sub(1111.1789)-32                   	 96477370	 13.21 ns/op	   0 B/op	 0 allocs/op
BenchmarkSub/123.456.Sub(0.123)-32                                                       	100000000	 10.75 ns/op	   0 B/op	 0 allocs/op
BenchmarkMul/1234.1234567890123456879.Mul(1111.1789)-32                                  	100000000	 10.43 ns/op	   0 B/op	 0 allocs/op
BenchmarkMul/1234.1234567890123456879.Mul(1111.1234567890123456789)-32                   	100000000	 10.49 ns/op	   0 B/op	 0 allocs/op
BenchmarkDiv/1234567890123456789.1234567890123456879.Div(1111.1789)-32                    	100000000	 10.46 ns/op	   0 B/op	 0 allocs/op
BenchmarkDiv/12345.1234567890123456879.Div(1111.1234567890123456789)-32                   	 50097552	 25.23 ns/op	   0 B/op	 0 allocs/op
BenchmarkDiv/123.456.Div(0.123)-32                                                        	111231829	 10.70 ns/op	   0 B/op	 0 allocs/op
BenchmarkDiv/548751.15465466546.Div(1542.456487)-32                                       	100000000	 10.30 ns/op	   0 B/op	 0 allocs/op
BenchmarkDivFallback/12345679012345679890123456789.1234567890123456789.Div(999999)-32     	  4187094	 302.1 ns/op	 264 B/op	 7 allocs/op
BenchmarkDivFallback/1234.Div(12345679012345679890123456789.1234567890123456789)-32       	  3901846	 306.7 ns/op	 320 B/op	 7 allocs/op
BenchmarkPow/1.01.Pow(10)-32                                                              	 30365348	 41.14 ns/op	   0 B/op	 0 allocs/op
BenchmarkPow/1.01.Pow(100)-32                                                            	   994129	  1137 ns/op	 817 B/op	13 allocs/op

# Parsing string
BenchmarkParse/1234567890123456789.1234567890123456879-32                                  	 32111433	 38.21 ns/op	   0 B/op	 0 allocs/op
BenchmarkParse/1234567890-32                                                               	 98585916	 12.58 ns/op	   0 B/op	 0 allocs/op
BenchmarkParse/0.1234567890123456879-32                                                    	 44339668	 26.45 ns/op	   0 B/op	 0 allocs/op
BenchmarkParseFallBack/123456789123456789123456.1234567890123456-32                        	  2805122	 473.3 ns/op	 192 B/op	 6 allocs/op
BenchmarkParseFallBack/111222333444555666777888999.1234567890123456789-32                  	  2442004	 500.8 ns/op	 216 B/op	 6 allocs/op
BenchmarkString/1234567890123456789.1234567890123456879-32                                 	 14577884	 76.50 ns/op	  48 B/op	 1 allocs/op
BenchmarkString/0.1234567890123456879-32                                                   	 41109242	 40.02 ns/op	  24 B/op	 1 allocs/op
BenchmarkStringFallBack/123456789123456789123456.1234567890123456-32                       	  4147044	 256.2 ns/op	 208 B/op	 4 allocs/op
BenchmarkStringFallBack/111222333444555666777888999.1234567890123456789-32                 	  3808071	 313.3 ns/op	 208 B/op	 4 allocs/op

# Marshal/Unmarshal
BenchmarkMarshalJSON/1234567890123456789.1234567890123456879-32                            	 13965998	 77.22 ns/op	  48 B/op	 1 allocs/op
BenchmarkMarshalJSON/0.1234567890123456879-32                                              	 24039360	 43.57 ns/op	  24 B/op	 1 allocs/op
BenchmarkMarshalJSON/12345678901234567891234567890123456789.1234567890123456879-32         	  3445560	 291.6 ns/op	 320 B/op	 5 allocs/op
BenchmarkUnmarshalJSON/1234567890123456789.1234567890123456879-32                          	 15943234	 73.77 ns/op	   0 B/op	 0 allocs/op
BenchmarkUnmarshalJSON/123456.123456-32                                                    	 46983879	 26.55 ns/op	   0 B/op	 0 allocs/op
BenchmarkUnmarshalJSON/12345678901234567891234567890123456789.1234567890123456879-32       	  2267604	 517.4 ns/op	 264 B/op	 6 allocs/op
BenchmarkMarshalBinary/1234567890123456789.1234567890123456879-32                          	 50875198	 25.97 ns/op	  24 B/op	 1 allocs/op
BenchmarkMarshalBinary/0.1234567890123456879-32                                            	 54470340	 20.91 ns/op	  16 B/op	 1 allocs/op
BenchmarkMarshalBinary/12345678901234567891234567890123456789.1234567890123456879-32       	 21138375	 48.85 ns/op	  32 B/op	 1 allocs/op
BenchmarkUnmarshalBinary/1234567890123456789.1234567890123456879-32                       	554818034	 2.034 ns/op	   0 B/op	 0 allocs/op
BenchmarkUnmarshalBinary/0.1234567890123456879-32                                          	637610913	 1.822 ns/op	   0 B/op	 0 allocs/op

Comparision with shopspring/decimal

Sec/op

                                                                               │   shopspring   │               udecimal               │
                                                                               │     sec/op     │    sec/op     vs base                │
Parse/1234567890123456789.1234567890123456879-32                                  384.35n ± 10%   38.55n ±  4%  -89.97% (p=0.000 n=10)
Parse/0.1234567890123456879-32                                                    262.30n ± 17%   25.93n ±  3%  -90.11% (p=0.000 n=10)
ParseFallBack/123456789123456789123456.1234567890123456-32                         373.2n ± 13%   418.3n ±  7%  +12.08% (p=0.000 n=10)
ParseFallBack/111222333444555666777888999.1234567890123456789-32                   418.5n ±  5%   453.4n ±  9%   +8.33% (p=0.003 n=10)
String/1234567890123456789.1234567890123456879-32                                 284.45n ± 26%   77.35n ± 10%  -72.81% (p=0.000 n=10)
String/0.1234567890123456879-32                                                   173.80n ± 15%   42.60n ±  6%  -75.49% (p=0.000 n=10)
StringFallBack/123456789123456789123456.1234567890123456-32                        394.1n ± 15%   268.4n ± 22%  -31.90% (p=0.000 n=10)
StringFallBack/111222333444555666777888999.1234567890123456789-32                  353.4n ± 26%   307.3n ±  6%  -13.04% (p=0.004 n=10)
Add/1234567890123456789.1234567890123456879.Add(1111.1789)-32                     384.65n ± 10%   10.87n ±  3%  -97.18% (p=0.000 n=10)
Add/123456.123456.Add(456781244.1324897546)-32                                    326.95n ± 13%   11.27n ±  2%  -96.55% (p=0.000 n=10)
Add/548751.15465466546.Add(1542.456487)-32                                        346.55n ± 10%   11.46n ±  3%  -96.69% (p=0.000 n=10)
Sub/123.456.Sub(0.123)-32                                                          64.67n ± 14%   10.79n ±  5%  -83.32% (p=0.000 n=10)
Sub/123456.123456.Sub(456781244.1324897546)-32                                    312.40n ± 16%   12.96n ±  4%  -95.85% (p=0.000 n=10)
Sub/548751.15465466546.Sub(1542.456487)-32                                        378.95n ±  7%   12.62n ±  4%  -96.67% (p=0.000 n=10)
Mul/1234.1234567890123456879.Mul(1111.1789)-32                                    111.80n ±  5%   10.44n ±  3%  -90.66% (p=0.000 n=10)
Mul/1234.1234567890123456879.Mul(1111.1234567890123456789)-32                     121.45n ± 11%   10.55n ±  4%  -91.31% (p=0.000 n=10)
Mul/123456.123456.Mul(456781244.1324897546)-32                                   100.300n ±  8%   9.964n ±  1%  -90.07% (p=0.000 n=10)
Mul/548751.15465466546.Mul(1542.456487)-32                                       103.200n ±  6%   9.943n ±  1%  -90.37% (p=0.000 n=10)
Div/1234567890123456789.1234567890123456879.Div(1111.1789)-32                     531.55n ±  9%   10.38n ±  3%  -98.05% (p=0.000 n=10)
Div/548751.15465466546.Div(1542.456487)-32                                        586.90n ± 12%   10.34n ±  3%  -98.24% (p=0.000 n=10)
DivFallback/12345679012345679890123456789.1234567890123456789.Div(999999)-32       762.1n ±  7%   302.9n ±  7%  -60.26% (p=0.000 n=10)
DivFallback/1234.Div(12345679012345679890123456789.1234567890123456789)-32         242.1n ± 87%   292.9n ±  4%        ~ (p=0.481 n=10)
Pow/1.01.Pow(10)-32                                                               724.70n ± 31%   40.83n ±  3%  -94.37% (p=0.000 n=10)
Pow/1.01.Pow(100)-32                                                               1.367µ ±  8%   1.062µ ±  7%  -22.28% (p=0.000 n=10)
MrshalJSON/1234567890123456789.1234567890123456879-32                             451.65n ±  6%   72.53n ± 12%  -83.94% (p=0.000 n=10)
MarshalJSON/0.1234567890123456879-32                                              204.70n ±  7%   43.49n ±  7%  -78.75% (p=0.000 n=10)
MarshalJSON/12345678901234567891234567890123456789.1234567890123456879-32          523.6n ± 15%   358.4n ±  7%  -31.55% (p=0.000 n=10)
UnmarshalJSON/1234567890123456789.1234567890123456879-32                          488.90n ± 10%   73.75n ±  1%  -84.92% (p=0.000 n=10)
UnmarshalJSON/0.1234567890123456879-32                                            449.70n ±  9%   40.97n ±  1%  -90.89% (p=0.000 n=10)
UnmarshalJSON/12345678901234567891234567890123456789.1234567890123456879-32        662.6n ±  9%   498.7n ±  8%  -24.75% (p=0.000 n=10)
MarshalBinary/1234567890123456789.1234567890123456879-32                           48.72n ± 21%   27.32n ±  5%  -43.92% (p=0.000 n=10)
MarshalBinary/0.1234567890123456879-32                                             46.45n ± 10%   20.70n ±  5%  -55.42% (p=0.000 n=10)
MarshalBinary/12345678901234567891234567890123456789.1234567890123456879-32        64.40n ± 33%   46.68n ±  5%        ~ (p=0.109 n=10)
UnmarshalBinary/1234567890123456789.1234567890123456879-32                        59.150n ± 31%   2.101n ±  2%  -96.45% (p=0.000 n=10)
UnmarshalBinary/0.1234567890123456879-32                                          45.120n ± 18%   1.891n ±  5%  -95.81% (p=0.000 n=10)
UnmarshalBinary/12345678901234567891234567890123456789.1234567890123456879-32      72.44n ± 11%   77.96n ±  4%        ~ (p=0.210 n=10)

B/op

                                                                               │ shopspring  │                udecimal                 │
                                                                               │    B/op     │    B/op     vs base                     │
Parse/1234567890123456789.1234567890123456879-32                                  168.0 ± 0%     0.0 ± 0%  -100.00% (p=0.000 n=10)
Parse/0.1234567890123456879-32                                                    144.0 ± 0%     0.0 ± 0%  -100.00% (p=0.000 n=10)
ParseFallBack/123456789123456789123456.1234567890123456-32                        168.0 ± 0%   192.0 ± 0%   +14.29% (p=0.000 n=10)
ParseFallBack/111222333444555666777888999.1234567890123456789-32                  168.0 ± 0%   216.0 ± 0%   +28.57% (p=0.000 n=10)
String/1234567890123456789.1234567890123456879-32                                240.00 ± 0%   48.00 ± 0%   -80.00% (p=0.000 n=10)
String/0.1234567890123456879-32                                                   80.00 ± 0%   24.00 ± 0%   -70.00% (p=0.000 n=10)
StringFallBack/123456789123456789123456.1234567890123456-32                       272.0 ± 0%   208.0 ± 0%   -23.53% (p=0.000 n=10)
StringFallBack/111222333444555666777888999.1234567890123456789-32                 272.0 ± 0%   208.0 ± 0%   -23.53% (p=0.000 n=10)
Add/1234567890123456789.1234567890123456879.Add(1111.1789)-32                     288.0 ± 0%     0.0 ± 0%  -100.00% (p=0.000 n=10)
Add/548751.15465466546.Add(1542.456487)-32                                        272.0 ± 0%     0.0 ± 0%  -100.00% (p=0.000 n=10)
Sub/123.456.Sub(0.123)-32                                                         40.00 ± 0%    0.00 ± 0%  -100.00% (p=0.000 n=10)
Sub/123456.123456.Sub(456781244.1324897546)-32                                    232.0 ± 0%     0.0 ± 0%  -100.00% (p=0.000 n=10)
Sub/548751.15465466546.Sub(1542.456487)-32                                        232.0 ± 0%     0.0 ± 0%  -100.00% (p=0.000 n=10)
Mul/1234.1234567890123456879.Mul(1111.1789)-32                                    96.00 ± 0%    0.00 ± 0%  -100.00% (p=0.000 n=10)
Mul/1234.1234567890123456879.Mul(1111.1234567890123456789)-32                     96.00 ± 0%    0.00 ± 0%  -100.00% (p=0.000 n=10)
Mul/548751.15465466546.Mul(1542.456487)-32                                        80.00 ± 0%    0.00 ± 0%  -100.00% (p=0.000 n=10)
Div/1234567890123456789.1234567890123456879.Div(1111.1789)-32                     352.0 ± 0%     0.0 ± 0%  -100.00% (p=0.000 n=10)
Div/548751.15465466546.Div(1542.456487)-32                                        368.0 ± 0%     0.0 ± 0%  -100.00% (p=0.000 n=10)
DivFallback/12345679012345679890123456789.1234567890123456789.Div(999999)-32      496.0 ± 0%   264.0 ± 0%   -46.77% (p=0.000 n=10)
DivFallback/1234.Div(12345679012345679890123456789.1234567890123456789)-32        272.0 ± 0%   320.0 ± 0%   +17.65% (p=0.000 n=10)
Pow/1.01.Pow(10)-32                                                               576.0 ± 0%     0.0 ± 0%  -100.00% (p=0.000 n=10)
Pow/1.01.Pow(100)-32                                                             1072.0 ± 0%   817.0 ± 0%   -23.79% (p=0.000 n=10)
MarshalJSON/1234567890123456789.1234567890123456879-32                           336.00 ± 0%   48.00 ± 0%   -85.71% (p=0.000 n=10)
MarshalJSON/0.1234567890123456879-32                                             104.00 ± 0%   24.00 ± 0%   -76.92% (p=0.000 n=10)
MarshalJSON/12345678901234567891234567890123456789.1234567890123456879-32         448.0 ± 0%   320.0 ± 0%   -28.57% (p=0.000 n=10)
UnmarshalJSON/1234567890123456789.1234567890123456879-32                          240.0 ± 0%     0.0 ± 0%  -100.00% (p=0.000 n=10)
UnmarshalJSON/123456.123456-32                                                    96.00 ± 0%    0.00 ± 0%  -100.00% (p=0.000 n=10)
UnmarshalJSON/12345678901234567891234567890123456789.1234567890123456879-32       272.0 ± 0%   264.0 ± 0%    -2.94% (p=0.000 n=10)
MarshalBinary/1234567890123456789.1234567890123456879-32                          48.00 ± 0%   24.00 ± 0%   -50.00% (p=0.000 n=10)
MarshalBinary/0.1234567890123456879-32                                            32.00 ± 0%   16.00 ± 0%   -50.00% (p=0.000 n=10)
UnmarshalBinary/1234567890123456789.1234567890123456879-32                        80.00 ± 0%    0.00 ± 0%  -100.00% (p=0.000 n=10)
UnmarshalBinary/0.1234567890123456879-32                                          40.00 ± 0%    0.00 ± 0%  -100.00% (p=0.000 n=10)
UnmarshalBinary/12345678901234567891234567890123456789.1234567890123456879-32     96.00 ± 0%   96.00 ± 0%         ~ (p=1.000 n=10)