Skip to content

Instantly share code, notes, and snippets.

@dougal
Created October 13, 2016 11:33
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 dougal/e972b26cd0293f99b41896e7d89c070e to your computer and use it in GitHub Desktop.
Save dougal/e972b26cd0293f99b41896e7d89c070e to your computer and use it in GitHub Desktop.
Custom date parser vs. Date.parse for Hyphenated ISO 8601 dates
require 'date'
require 'benchmark/ips'
def custom_date_parse(str)
Date.new(str[0,4].to_i, str[5,2].to_i, str[8,2].to_i)
end
Benchmark.ips do |x|
x.time = 5
x.warmup = 2
x.report('Date.parse') do |count|
count.times do
Date.parse('2016-10-13')
end
end
x.report('Custom parser') do |count|
count.times do
custom_date_parse('2016-10-13')
end
end
end
# $ ruby date_bm.rb
# Warming up --------------------------------------
# Date.parse 16.088k i/100ms
# Custom parser 77.894k i/100ms
# Calculating -------------------------------------
# Date.parse 175.639k (± 2.6%) i/s - 884.840k in 5.041341s
# Custom parser 1.098M (± 2.8%) i/s - 5.530M in 5.041368s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment