Skip to content

Instantly share code, notes, and snippets.

Created January 3, 2013 04:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/4440875 to your computer and use it in GitHub Desktop.
Save anonymous/4440875 to your computer and use it in GitHub Desktop.
change active_support String#to_date
require 'benchmark'
class String
def old_to_date
unless blank?
date_values = ::Date._parse(self, false).values_at(:year, :mon, :mday)
::Date.new(*date_values)
end
end
def new_to_date
::Date.parse(self, false) unless blank?
end
def blank?
false
end
end
date = '1/2/3'
Benchmark.bm do |x|
x.report('old') { 100000.times { date.old_to_date }}
x.report('new') { 100000.times { date.new_to_date }}
end
=> results
user system total real
old 0.950000 0.000000 0.950000 ( 0.952705)
new 0.940000 0.000000 0.940000 ( 0.945149)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment