-
Notifications
You must be signed in to change notification settings - Fork 374
Expand file tree
/
Copy pathplus-vs-to_f-plus.rb
More file actions
33 lines (26 loc) · 880 Bytes
/
plus-vs-to_f-plus.rb
File metadata and controls
33 lines (26 loc) · 880 Bytes
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
30
31
32
33
require 'benchmark/ips'
TimeFloatLowPrecision = 1525262447.0
OffsetFloatLowPrecision = 1.1
TimeFloatHighPrecision = 1525262447.1234567
OffsetFloatHighPrecision = 1.1234567
TimeLowPrecision = Time.at(TimeFloatLowPrecision)
TimeHighPrecision = Time.at(TimeFloatHighPrecision)
def fast_low_precision
Time.at(TimeLowPrecision.to_f + OffsetFloatLowPrecision)
end
def slow_low_precision
TimeLowPrecision + OffsetFloatLowPrecision
end
def fast_high_precision
Time.at(TimeHighPrecision.to_f + OffsetFloatHighPrecision)
end
def slow_high_precision
TimeHighPrecision + OffsetFloatHighPrecision
end
Benchmark.ips do |x|
x.report('Time#to_f+ (low)', 'fast_low_precision;' * 1000)
x.report('Time#+ (low)', 'slow_low_precision;' * 1000)
x.report('Time#to_f+ (high)', 'fast_high_precision;' * 1000)
x.report('Time#+ (high)', 'slow_high_precision;' * 1000)
x.compare!
end