Registry Mirror
搜索
Skip to content
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
Files
Expand file tree
main
Breadcrumbs
fast-ruby
/
code
/
bigdecimal
/
integer-string-vs-numeric.rb
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
29 lines (24 loc) · 567 Bytes
main
Breadcrumbs
fast-ruby
/
code
/
bigdecimal
/
integer-string-vs-numeric.rb
Copy path
Top
File metadata and controls
Code
Blame
29 lines (24 loc) · 567 Bytes
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
require 'benchmark/ips'
require 'bigdecimal'
require 'bigdecimal/util'
# On Ruby 4.0, building from the Integer is about 2x faster than from a String.
def faster
BigDecimal(1)
end
def fast
1.to_d
end
# BigDecimal('1') and '1'.to_d parse the String the same way, so these two should tie.
def slow
BigDecimal('1')
end
def slower
'1'.to_d
end
Benchmark.ips do |x|
x.report('integer new') { faster }
x.report('integer to_d') { fast }
x.report('integer string new') { slow }
x.report('integer string to_d') { slower }
x.compare!
end
You can’t perform that action at this time.