Skip to content

Commit 58b4b51

Browse files
committed
Add BigDecimal benchmarks
1 parent 013fa34 commit 58b4b51

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ Idioms
4646

4747
- [General](#general)
4848
- [Array](#array)
49+
- [BigDecimal](#bigdecimal)
4950
- [Date](#date)
5051
- [Enumerable](#enumerable)
5152
- [Hash](#hash)
@@ -655,6 +656,34 @@ Comparison:
655656
inject block: 14063.1 i/s - 1.35x slower
656657
```
657658

659+
### BigDecimal
660+
661+
##### `BigDecimal('1')` vs `BigDecimal('1.0')` vs `'1'.to_d` vs `'1.0'.to_d` vs `BigDecimal(1)` vs `1.to_d` vs `BigDecimal(1.0, 2)` vs `1.0.to_d` [code](code/bigdecimal/string-vs-numeric.rb)
662+
663+
```
664+
$ ruby -v code/bigdecimal/string-vs-numeric.rb
665+
ruby 2.5.3p105 (2018-10-18 revision 65156) [x86_64-darwin18]
666+
Calculating -------------------------------------
667+
integer string new 2.497M (± 2.0%) i/s - 12.645M in 5.066385s
668+
float string new 2.414M (± 2.0%) i/s - 12.182M in 5.048473s
669+
integer string to_d 2.402M (± 1.8%) i/s - 12.010M in 5.001784s
670+
float string to_d 2.318M (± 1.8%) i/s - 11.663M in 5.032429s
671+
integer new 1.601M (± 1.2%) i/s - 8.022M in 5.010201s
672+
integer to_d 1.563M (± 1.8%) i/s - 7.817M in 5.001726s
673+
float to_d 529.413k (± 3.6%) i/s - 2.671M in 5.051477s
674+
float new 517.635k (± 4.5%) i/s - 2.604M in 5.042605s
675+
676+
Comparison:
677+
integer string new: 2496860.3 i/s
678+
float string new: 2414122.8 i/s - same-ish: difference falls within error
679+
integer string to_d: 2401929.3 i/s - 1.04x slower
680+
float string to_d: 2318272.7 i/s - 1.08x slower
681+
integer new: 1601402.4 i/s - 1.56x slower
682+
integer to_d: 1563335.6 i/s - 1.60x slower
683+
float to_d: 529412.7 i/s - 4.72x slower
684+
float new: 517634.5 i/s - 4.82x slower
685+
```
686+
658687
### Date
659688

660689
##### `Date.iso8601` vs `Date.parse` [code](code/date/iso8601-vs-parse.rb)

code/bigdecimal/string-vs-numeric.rb

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
require 'benchmark/ips'
2+
require 'bigdecimal'
3+
require 'bigdecimal/util'
4+
5+
def fastest
6+
BigDecimal('1')
7+
end
8+
9+
def fastest2
10+
BigDecimal('1.0')
11+
end
12+
13+
def fast
14+
'1'.to_d
15+
end
16+
17+
def fast2
18+
'1.0'.to_d
19+
end
20+
21+
def slow
22+
BigDecimal(1)
23+
end
24+
25+
def slow2
26+
1.to_d
27+
end
28+
29+
def slowest
30+
BigDecimal(1.0, 2)
31+
end
32+
33+
def slowest2
34+
1.0.to_d
35+
end
36+
37+
38+
Benchmark.ips do |x|
39+
x.report('integer string new') { fastest }
40+
x.report('float string new') { fastest2 }
41+
x.report('integer string to_d') { fast }
42+
x.report('float string to_d') { fast2 }
43+
x.report('integer new') { slow }
44+
x.report('integer to_d') { slow2 }
45+
x.report('float to_d') { slowest2 }
46+
x.report('float new') { slowest }
47+
x.compare!
48+
end

0 commit comments

Comments
 (0)