Skip to content

Instantly share code, notes, and snippets.

@parkr
Created December 17, 2014 19:09
Show Gist options
  • Save parkr/921faecc359f838f133d to your computer and use it in GitHub Desktop.
Save parkr/921faecc359f838f133d to your computer and use it in GitHub Desktop.
require 'benchmark/ips'
def alter(obj)
return obj if obj.respond_to?(:strftime)
obj.downcase! if obj.is_a?(String)
case obj
when 'now'.freeze, 'today'.freeze
Time.now
when /\A\d+\z/, Integer
Time.at(obj.to_i)
when String
Time.parse(obj)
else
nil
end
rescue ArgumentError
nil
end
def neuer(obj)
return obj if obj.respond_to?(:strftime)
case obj
when /\A(?:now|today)\z/i
Time.now
when /\A\d+\z/, Integer
Time.at(obj.to_i)
when String
Time.parse(obj)
else
nil
end
rescue ArgumentError
nil
end
[ 'Now', 'Today', 'now', 'today', '1418842869', nil ].each do |arg|
Benchmark.ips do |x|
x.report("old #{arg.inspect}") { alter(arg) }
x.report("new #{arg.inspect}") { neuer(arg) }
x.compare!
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment