Skip to content

Instantly share code, notes, and snippets.

@pascalbetz
Created May 7, 2021 14:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pascalbetz/efe2c12861d1f3c167a6c00a1ca1f81a to your computer and use it in GitHub Desktop.
Save pascalbetz/efe2c12861d1f3c167a6c00a1ca1f81a to your computer and use it in GitHub Desktop.
Benchmark Ruby Date Operations
require "benchmark/ips"
require "date"
base = Date.new(Date.today.year)
Benchmark.ips do |x|
x.compare!
x.report("int 1") { base + (100) }
x.report("int 2") { base + (100 - 1) }
x.report("int 3") { base + (100 - 1 - 1) }
x.report("int 4") { base + (100 - 1 - 1 - 1) }
x.report("int 5") { base + (100 - 1 - 1 - 1 - 1) }
x.report("date 1") { base + 100 - 1 }
x.report("date 2") { base + 100 - 1 }
x.report("date 3") { base + 100 - 1 - 1 }
x.report("date 4") { base + 100 - 1 - 1 }
x.report("date 5") { base + 100 - 1 - 1 }
end
# Comparison:
# int 2: 4327503.9 i/s
# int 1: 4313658.2 i/s - same-ish: difference falls within error
# int 5: 4128303.4 i/s - 1.05x (± 0.00) slower
# int 3: 4115620.3 i/s - 1.05x (± 0.00) slower
# int 4: 4110523.2 i/s - same-ish: difference falls within error
# date 1: 1987276.0 i/s - 2.18x (± 0.00) slower
# date 2: 1951796.9 i/s - 2.22x (± 0.00) slower
# date 3: 1275165.1 i/s - 3.39x (± 0.00) slower
# date 4: 1273855.2 i/s - 3.40x (± 0.00) slower
# date 5: 1262090.7 i/s - 3.43x (± 0.00) slower
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment