Skip to content

Performance

udecimal compared with shopspring, ericlagergren, apd and govalues on an Apple M4. Unless noted, inputs have at most 19 digits.

  • Add, Sub, Mul and Div don't allocate and are 4.5–7.7× faster than shopspring, 1.9–5.1× faster than ericlagergren and 3.1–8.1× faster than apd.
  • govalues is about as fast on Add, faster on Sub, String and MarshalText, and 20.6× slower on Div.
  • shopspring is faster on Pow with an exponent of 100.
  • Over 19 digits, govalues can't represent the values. udecimal is the fastest up to 38 digits, and in 1 of the 6 cases beyond that.

Each library's time as a multiple of udecimal's. Every value is the geometric mean of the median times over a benchmark's cases.

Benchmark udecimal shopspring ericlagergren apd govalues
Add 4.4 ns 5.4× 2.8× 4.4× 1.1×
Sub 6.3 ns 4.5× 1.9× 3.1× 0.87×
Mul 4.2 ns 5.1× 2.5× 5.3× 3.4×
Div 9.9 ns 7.7× 5.1× 8.1× 20.6×
Pow 124 ns 0.83× 2.5× 5.1× 3.2×
Parse 11.9 ns 3.9× 4.1× 4.8× 2.0×
String 17.6 ns 2.7× 2.4× 1.1× 0.62×
MarshalText 18.1 ns 2.7× 2.6× 1.5× 0.49×
UnmarshalText 12.7 ns 4.6× 4.1× 5.5× 2.0×
MarshalJSON 76.6 ns 1.6× 1.5× 1.2× 1.1×
UnmarshalJSON 77.4 ns 1.7× 1.7× 1.8× 1.2×

Values in bold are faster than udecimal.

Arithmetic

By geometric mean, udecimal is the fastest on Add, Mul and Div, and govalues is the fastest on Sub (5.5 ns, against 6.3 ns for udecimal). govalues is also slightly faster in three individual Add cases and three Mul cases.

Add
4.4 ns udecimal 23.7 ns shop- spring 12.5 ns eric- lagergren 19.2 ns apd 4.7 ns govalues
Sub
6.3 ns udecimal 28.6 ns shop- spring 12.1 ns eric- lagergren 19.4 ns apd 5.5 ns govalues
Mul
4.2 ns udecimal 21.4 ns shop- spring 10.4 ns eric- lagergren 22.3 ns apd 14.3 ns govalues
Div
9.9 ns udecimal 76.4 ns shop- spring 50.8 ns eric- lagergren 80.3 ns apd 204 ns govalues

govalues' Mul takes about 4 ns when the product fits in 19 digits. In the last two cases the product is longer, so govalues rounds it to 19 digits, which takes 91–97 ns; the other libraries keep the full product. govalues' Div takes 193–212 ns and allocates twice in every case.

All cases
Case udecimal shopspring ericlagergren apd govalues
Add 123.456.Add(0.123) 3.2 ns / 0 18.7 ns / 2 12.2 ns / 0 19.0 ns / 0 4.2 ns / 0
Add 3.Add(7) 3.2 ns / 0 18.9 ns / 2 12.2 ns / 0 18.8 ns / 0 4.2 ns / 0
Add 123456.123456.Add(999999) 5.5 ns / 0 27.1 ns / 2 12.4 ns / 0 23.7 ns / 0 5.0 ns / 0
Add 123456.123456.Add(456781244.1324897546) 5.2 ns / 0 26.9 ns / 2 12.8 ns / 0 14.0 ns / 0 5.0 ns / 0
Add 548751.15465466546.Add(1542.456487) 5.5 ns / 0 28.8 ns / 2 12.6 ns / 0 22.0 ns / 0 5.0 ns / 0
Sub 123.456.Sub(0.123) 4.9 ns / 0 19.6 ns / 2 12.2 ns / 0 19.0 ns / 0 4.9 ns / 0
Sub 3.Sub(7) 5.0 ns / 0 20.3 ns / 2 11.8 ns / 0 13.4 ns / 0 5.1 ns / 0
Sub 123456.123456.Sub(999999) 7.8 ns / 0 35.5 ns / 2 12.3 ns / 0 30.3 ns / 0 6.0 ns / 0
Sub 123456.123456.Sub(456781244.1324897546) 7.1 ns / 0 36.0 ns / 2 12.2 ns / 0 15.6 ns / 0 5.9 ns / 0
Sub 548751.15465466546.Sub(1542.456487) 7.5 ns / 0 37.5 ns / 2 12.2 ns / 0 23.0 ns / 0 5.7 ns / 0
Mul 123.456.Mul(0.123) 4.2 ns / 0 28.7 ns / 2 9.0 ns / 0 25.7 ns / 0 4.0 ns / 0
Mul 3.Mul(7) 4.2 ns / 0 19.9 ns / 2 8.8 ns / 0 13.9 ns / 0 4.1 ns / 0
Mul 123456.123456.Mul(999999) 4.2 ns / 0 19.8 ns / 2 8.7 ns / 0 25.7 ns / 0 4.1 ns / 0
Mul 123456.123456.Mul(456781244.1324897546) 4.2 ns / 0 19.6 ns / 2 13.0 ns / 0 20.1 ns / 0 91.2 ns / 0
Mul 548751.15465466546.Mul(1542.456487) 4.2 ns / 0 20.3 ns / 2 13.1 ns / 0 30.2 ns / 0 97.3 ns / 0
Div 123.456.Div(0.123) 10.5 ns / 0 73.6 ns / 7 49.9 ns / 2 88.6 ns / 2 212 ns / 2
Div 3.Div(7) 9.4 ns / 0 76.2 ns / 7 45.2 ns / 2 68.4 ns / 2 202 ns / 2
Div 123456.123456.Div(999999) 9.4 ns / 0 73.9 ns / 7 53.4 ns / 2 94.7 ns / 2 203 ns / 2
Div 123456.123456.Div(456781244.1324897546) 9.8 ns / 0 79.1 ns / 7 54.0 ns / 2 81.1 ns / 2 193 ns / 2
Div 548751.15465466546.Div(1542.456487) 10.6 ns / 0 79.4 ns / 7 52.3 ns / 2 71.7 ns / 2 211 ns / 2

Each cell shows the median ns/op and allocs/op.

Parsing and formatting

udecimal parses 2.0–4.8× faster than the other libraries. Formatting is closer: govalues' String is faster (0.62× udecimal's time), and apd's is similar overall (1.1×) and faster in 3 of the 4 cases.

Parse
11.9 ns udecimal 46.1 ns shop- spring 49.2 ns eric- lagergren 56.9 ns apd 23.6 ns govalues
String
17.6 ns udecimal 47.8 ns shop- spring 42.4 ns eric- lagergren 18.6 ns apd 10.8 ns govalues
All cases, including MarshalText and UnmarshalText
Case udecimal shopspring ericlagergren apd govalues
Parse 123 6.9 ns / 0 21.3 ns / 2 27.8 ns / 1 41.1 ns / 0 11.1 ns / 0
Parse 123456.123456 13.3 ns / 0 37.1 ns / 2 56.7 ns / 1 74.5 ns / 1 28.2 ns / 0
Parse 1234567890 10.7 ns / 0 32.4 ns / 2 46.5 ns / 1 43.7 ns / 0 23.2 ns / 0
Parse 0.1234567890123456879 20.3 ns / 0 177 ns / 5 80.2 ns / 1 78.5 ns / 1 43.0 ns / 0
String 123 13.7 ns / 1 39.9 ns / 2 38.2 ns / 4 13.5 ns / 0 6.7 ns / 0
String 123456.123456 20.2 ns / 1 56.6 ns / 2 45.9 ns / 4 17.2 ns / 0 12.1 ns / 0
String 1234567890 15.6 ns / 1 38.8 ns / 2 40.1 ns / 4 14.5 ns / 0 9.4 ns / 0
String 0.1234567890123456879 22.2 ns / 1 59.4 ns / 2 46.0 ns / 4 36.0 ns / 2 18.0 ns / 0
MarshalText 123 14.1 ns / 1 40.5 ns / 2 45.3 ns / 4 22.1 ns / 1 4.2 ns / 0
MarshalText 123456.123456 20.5 ns / 1 57.1 ns / 2 50.0 ns / 4 25.3 ns / 1 10.5 ns / 0
MarshalText 1234567890 15.9 ns / 1 40.2 ns / 2 44.4 ns / 4 21.5 ns / 1 7.8 ns / 0
MarshalText 0.1234567890123456879 23.5 ns / 1 62.5 ns / 2 49.1 ns / 4 42.8 ns / 3 18.6 ns / 0
UnmarshalText 123 8.0 ns / 0 29.8 ns / 3 31.1 ns / 1 48.8 ns / 1 12.6 ns / 0
UnmarshalText 123456.123456 13.9 ns / 0 47.1 ns / 3 58.3 ns / 1 86.2 ns / 2 29.7 ns / 0
UnmarshalText 1234567890 11.6 ns / 0 42.7 ns / 3 48.1 ns / 1 58.5 ns / 1 24.0 ns / 0
UnmarshalText 0.1234567890123456879 20.0 ns / 0 189 ns / 6 82.3 ns / 1 94.8 ns / 2 41.9 ns / 0

Each cell shows the median ns/op and allocs/op.

JSON

Most of the time is spent in encoding/json: udecimal's String takes 17.6 ns, and json.Marshal of a udecimal value takes 76.6 ns. The differences between libraries shrink to 1.1–1.8×, and udecimal is the fastest in both directions.

MarshalJSON
76.6 ns udecimal 121 ns shop- spring 118 ns eric- lagergren 91.0 ns apd 82.1 ns govalues
UnmarshalJSON
77.4 ns udecimal 131 ns shop- spring 129 ns eric- lagergren 143 ns apd 91.9 ns govalues
All cases
Case udecimal shopspring ericlagergren apd govalues
MarshalJSON 123 69.6 ns / 2 113 ns / 4 113 ns / 5 82.8 ns / 2 77.5 ns / 2
MarshalJSON 123456.123456 79.5 ns / 2 127 ns / 4 126 ns / 5 89.8 ns / 2 82.9 ns / 2
MarshalJSON 1234567890 72.4 ns / 2 108 ns / 4 114 ns / 5 86.9 ns / 2 76.3 ns / 2
MarshalJSON 0.1234567890123456879 86.2 ns / 2 136 ns / 4 118 ns / 5 106 ns / 4 92.6 ns / 2
UnmarshalJSON 123 64.2 ns / 0 88.2 ns / 3 100 ns / 1 116 ns / 1 70.2 ns / 0
UnmarshalJSON 123456.123456 77.5 ns / 0 118 ns / 3 134 ns / 1 165 ns / 2 97.7 ns / 0
UnmarshalJSON 1234567890 72.9 ns / 0 105 ns / 3 122 ns / 1 130 ns / 1 89.2 ns / 0
UnmarshalJSON 0.1234567890123456879 98.7 ns / 0 265 ns / 6 168 ns / 1 168 ns / 2 117 ns / 0

Each cell shows the median ns/op and allocs/op.

Pow

The libraries keep different numbers of digits in a Pow result. 1.01^100 has 200 decimal places: shopspring's PowInt32 keeps all of them, udecimal keeps 19 decimal places, govalues 19 digits in total, and ericlagergren and apd 38 significant digits. udecimal is the fastest for 1.01^10. shopspring is faster for 1.01^100, where udecimal takes 433 ns and allocates 13 times.

Case udecimal shopspring ericlagergren apd govalues
Pow 1.01.Pow(10) 35.7 ns / 0 65.4 ns / 5 100 ns / 0 274 ns / 1 203 ns / 0
Pow 1.01.Pow(100) 433 ns / 13 161 ns / 6 941 ns / 18 1482 ns / 33 799 ns / 8

Each cell shows the median ns/op and allocs/op.

Beyond 19 digits

These inputs have more than 19 significant digits, which is more than govalues can hold. govalues either rounds them during parsing without returning an error, or rejects them, so it has no results in this section.

20 to 38 digits

These values still fit in udecimal's 128-bit representation. Parsing and arithmetic don't allocate, and udecimal is the fastest in every case.

Parse
26.8 ns udecimal 255 ns shop- spring 351 ns eric- lagergren 319 ns apd can't represent govalues
String
32.8 ns udecimal 94.3 ns shop- spring 129 ns eric- lagergren 122 ns apd can't represent govalues
Add
4.2 ns udecimal 26.4 ns shop- spring 28.2 ns eric- lagergren 23.0 ns apd can't represent govalues
Sub
6.0 ns udecimal 34.5 ns shop- spring 32.6 ns eric- lagergren 31.2 ns apd can't represent govalues
Mul
11.3 ns udecimal 32.0 ns shop- spring 30.2 ns eric- lagergren 61.5 ns apd can't represent govalues
Div
16.6 ns udecimal 137 ns shop- spring 119 ns eric- lagergren 225 ns apd can't represent govalues
All cases
Case udecimal shopspring ericlagergren apd govalues
ParseWide 1234567890123456789.1234567890123456879 27.5 ns / 0 271 ns / 5 371 ns / 4 340 ns / 4 can't represent
ParseWide 12345678901234567890123456789.123 26.1 ns / 0 240 ns / 5 331 ns / 4 299 ns / 4 can't represent
StringWide 1234567890123456789.1234567890123456879 36.9 ns / 1 102 ns / 4 137 ns / 6 138 ns / 6 can't represent
StringWide 12345.1234567890123456789 29.1 ns / 1 87.2 ns / 3 122 ns / 7 108 ns / 4 can't represent
AddWide 1234567890123456789.1234567890123456879.Add(1111.1789) 5.5 ns / 0 30.0 ns / 2 35.2 ns / 0 29.2 ns / 0 can't represent
AddWide 1234567890123456789.1234567890123456879.Add(1234567890123456789.1234567890123456789) 3.3 ns / 0 23.2 ns / 2 22.5 ns / 0 18.2 ns / 0 can't represent
SubWide 1234567890123456789.1234567890123456879.Sub(1111.1789) 7.0 ns / 0 38.7 ns / 2 41.1 ns / 0 30.3 ns / 0 can't represent
SubWide 1234567890123456789.1234567890123456879.Sub(1234567890123456789.1234567890123456789) 5.1 ns / 0 30.7 ns / 2 25.9 ns / 0 32.1 ns / 0 can't represent
MulWide 1234.1234567890123456879.Mul(1111.1789) 10.1 ns / 0 23.0 ns / 2 14.8 ns / 0 33.7 ns / 0 can't represent
MulWide 1234.1234567890123456879.Mul(1111.1234567890123456789) 12.7 ns / 0 44.6 ns / 2 61.4 ns / 1 112 ns / 2 can't represent
DivWide 1234567890123456789.1234567890123456879.Div(1111.1789) 13.5 ns / 0 85.3 ns / 7 59.3 ns / 2 225 ns / 8 can't represent
DivWide 12345.1234567890123456879.Div(1111.1234567890123456789) 19.2 ns / 0 156 ns / 8 160 ns / 3 224 ns / 8 can't represent
DivWide 1234567890123456789.1234567890123456879.Div(9876543210987654321.1234567890123456789) 14.1 ns / 0 147 ns / 8 140 ns / 3 212 ns / 7 can't represent
DivWide 22773757910726981402256170801141121114.Div(811656739243220271.159) 21.0 ns / 0 181 ns / 8 149 ns / 3 239 ns / 9 can't represent

Each cell shows the median ns/op and allocs/op.

govalues errors

The reasons recorded by the benchmark. parsed X as Y means govalues returned Y for the input X.

  • parsed 1234567890123456789.1234567890123456879 as 1234567890123456789
  • parsing decimal: decimal overflow: the integer part of a decimal.Decimal can have at most 19 digits, but it has 29 digits
  • parsing decimal: decimal overflow: the integer part of a decimal.Decimal can have at most 19 digits, but it has 24 digits
  • parsing decimal: decimal overflow: the integer part of a decimal.Decimal can have at most 19 digits, but it has 27 digits
  • parsed 12345.1234567890123456789 as 12345.12345678901235
  • parsed 1234.1234567890123456879 as 1234.123456789012346
  • parsed 12345.1234567890123456879 as 12345.12345678901235
  • parsing decimal: decimal overflow: the integer part of a decimal.Decimal can have at most 19 digits, but it has 38 digits

More than 38 digits

These cases have an operand of 40 to 48 digits, which doesn't fit in 128 bits, so udecimal uses math/big. It allocates 4–7 times per operation, is the fastest in 1 of the 6 cases, and is up to 15% slower than the fastest library in the others.

All cases
Case udecimal shopspring ericlagergren apd govalues
ParseWide 123456789123456789123456.1234567890123456 290 ns / 6 284 ns / 5 394 ns / 4 382 ns / 6 can't represent
ParseWide 111222333444555666777888999.1234567890123456789 292 ns / 6 319 ns / 5 440 ns / 4 460 ns / 6 can't represent
StringWide 123456789123456789123456.1234567890123456 123 ns / 4 122 ns / 4 143 ns / 6 159 ns / 6 can't represent
StringWide 111222333444555666777888999.1234567890123456789 125 ns / 4 122 ns / 4 145 ns / 6 164 ns / 6 can't represent
DivWide 12345679012345679890123456789.1234567890123456789.Div(999999) 71.5 ns / 6 78.0 ns / 7 62.1 ns / 2 260 ns / 9 can't represent
DivWide 1234.Div(12345679012345679890123456789.1234567890123456789) 74.3 ns / 7 65.0 ns / 5 152 ns / 4 287 ns / 9 can't represent

Each cell shows the median ns/op and allocs/op.

Allocations

Allocations per operation. udecimal's Add, Sub, Mul, Div and parsing don't allocate. String, MarshalText and MarshalJSON allocate the string or byte slice they return. A range means the number differs between cases.

Benchmark udecimal shopspring ericlagergren apd govalues
Add 0 2 0 0 0
Sub 0 2 0 0 0
Mul 0 2 0 0 0
Div 0 7 2 2 2
Pow 0–13 5–6 0–18 1–33 0–8
Parse 0 2–5 1 0–1 0
String 1 2 4 0–2 0
MarshalText 1 2 4 1–3 0
UnmarshalText 0 3–6 1 1–2 0
MarshalJSON 2 4 5 2–4 2
UnmarshalJSON 0 3–6 1 1–2 0

Methodology

The benchmarks ran on an Apple M4 (darwin/arm64) with Go 1.27.4 on 2026-11-20, from 09:26 to 11:16 UTC, using go test -run NONE -bench=./<library> -benchmem -count=10 -timeout=2h -v once per library (Makefile). Each value is the median of 10 runs. The charts and the summary table use the geometric mean of these medians over each benchmark's cases.

Results depend on the hardware. To run the benchmarks yourself, see benchmarks/README.md.

Library versions and configuration
Library Version Configuration
udecimal v1.12.0 defaults: up to 19 decimal places, extra digits truncated
shopspring/decimal v1.6.0 DivisionPrecision = 19, so Div keeps 19 decimal places like udecimal; shopspring rounds the last digit half away from zero. Pow is PowInt32.
ericlagergren/decimal v0.0.0-20240411145413-00de7ca16731 results: 38 significant digits, ToZero (truncate). Inputs are parsed at precision 64, so they are read exactly. Results are written to a separate z, never to an operand.
cockroachdb/apd v3.2.3 BaseContext.WithPrecision(38), RoundDown (truncate)
govalues/decimal v0.1.36 not configurable: results are rounded to 19 significant digits, half to even

All benchmarks follow these rules (source):

  • Inputs are parsed exactly. Before timing, each library's parsed value is compared with the input. A mismatch fails the benchmark, and the report generator rejects the run. The exception is Beyond 19 digits, where govalues is reported as "can't represent".
  • Only operations that all five libraries provide are compared. JSON is encoded with json.Marshal and json.Unmarshal, because apd has no JSON methods and ericlagergren has no MarshalJSON. Binary encoding isn't compared, because apd and ericlagergren don't implement it.
  • Results aren't compared between libraries, only inputs. Results can differ in the last digits because the libraries keep different numbers of digits.

Raw results: