Skip to content

Instantly share code, notes, and snippets.

@stopdropandrew
Last active September 10, 2016 21:47
Show Gist options
  • Save stopdropandrew/7fa9b1f36dcd3ef528094972d4b6eee8 to your computer and use it in GitHub Desktop.
Save stopdropandrew/7fa9b1f36dcd3ef528094972d4b6eee8 to your computer and use it in GitHub Desktop.
more optimization!
Benchmark.ips do |x|
strings = ['android', '1002,60009,30012,60004,20023,10005,10011,50003,10012,50004,10005,30013,60007,10006', '4068B063-0CDF-4579-B4E9-15298ECF9929', 'android', '1002,60009,30012,60004,20023,10005,10011,50003,10012,50004,10005,30013,60007,10006', '4068B063-0CDF-4579-B4E9-15298ECF9929', 'android', '1002,60009,30012,60004,20023,10005,10011,50003,10012,50004,10005,30013,60007,10006', '4068B063-0CDF-4579-B4E9-15298ECF9929']
times = ['2016-08-01 13:00:01+0000', '2016-08-01T13:00:01+0000', '2015-09-27T16:36:45.966+0000']
10.times do
times += strings
end
pt = times.map { |t| Time.parse(t) rescue nil }
x.report("match then build w/ iso") do
times.each_with_index do |time, i|
m = time.match(/^(\d+-\d+-\d+)[T ](\d+:\d+:\d+[.0-9]*)/)
next unless m
t = Time.iso8601("#{m[1]}T#{m[2]}+00:00")
raise unless pt[i] == t
end
end
x.report("match then build w/ new") do
times.each_with_index do |time, i|
m = time.match(/^(\d{4})-(\d{2})-(\d{2})[T ](\d{2}):(\d{2}):(\d{2}(?:\.\d+)?)/)
next unless m
t = Time.new(*m[1..5], m[6].to_r, 0)
raise unless pt[i] == t
end
end
x.report("match then build w/ new + early skip") do
times.each_with_index do |time, i|
next unless time[4] == '-' && (year = time[0..3].to_i) && year > 2000
m = time.match(/^(\d{4})-(\d{2})-(\d{2})[T ](\d{2}):(\d{2}):(\d{2}(?:\.\d+)?)/)
t = Time.new(*m[1..5], m[6].to_r, 0)
raise unless pt[i] == t
end
end
x.compare!
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment