Created
December 17, 2014 19:09
-
-
Save parkr/921faecc359f838f133d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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