Last active
March 5, 2021 09:30
-
-
Save tomquas/36ca062b8f13d0e30a7e218eb99b28a4 to your computer and use it in GitHub Desktop.
compares several ways to calculate the number of days passed since epoch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'date' | |
require 'benchmark/ips' | |
Benchmark.ips do |x| | |
# Configure the number of seconds used during | |
# the warmup phase (default 2) and calculation phase (default 5) | |
x.config(:time => 5, :warmup => 2) | |
# These parameters can also be configured this way | |
x.time = 5 | |
x.warmup = 2 | |
# Typical mode, runs the block as many times as it can | |
x.report(1) { Date.today.to_time.to_i / (60 * 60 * 24) } | |
x.report(2) { Date.today.to_time.to_i / 86400 } | |
x.report(3) { (Date.today.to_time.to_f / 86400.0).to_i } | |
x.report(4) { (Time.now.to_f / 86400.0).to_i } | |
end | |
shito> bundle exec ruby test/days_since_epoch_bench.rb | |
Warming up -------------------------------------- | |
1 37.304k i/100ms | |
2 31.911k i/100ms | |
3 31.645k i/100ms | |
4 235.326k i/100ms | |
Calculating ------------------------------------- | |
1 355.753k (± 2.8%) i/s - 1.791M in 5.037189s | |
2 357.397k (± 3.6%) i/s - 1.787M in 5.006714s | |
3 332.356k (± 4.1%) i/s - 1.677M in 5.054967s | |
4 2.352M (± 3.9%) i/s - 11.766M in 5.011465s | |
shito> bundle exec ruby test/days_since_epoch_bench.rb | |
Warming up -------------------------------------- | |
1 37.145k i/100ms | |
2 35.933k i/100ms | |
3 34.502k i/100ms | |
4 238.409k i/100ms | |
Calculating ------------------------------------- | |
1 374.085k (± 2.1%) i/s - 1.894M in 5.066361s | |
2 373.731k (± 2.7%) i/s - 1.869M in 5.003710s | |
3 362.250k (± 2.5%) i/s - 1.829M in 5.051346s | |
4 2.424M (± 2.0%) i/s - 12.159M in 5.018387s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment