Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.