Created
March 2, 2014 02:44
-
-
Save eddanger/9301090 to your computer and use it in GitHub Desktop.
Optimize time range comparisons using step
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
start1 = Time.parse('01:00') | |
end1 = Time.parse('10:00') | |
timespan1 = (start1.to_i..(end1.to_i - 1)) | |
timespan1b = (start1.to_i..(end1.to_i - 1)).step(1.minute) | |
start2 = Time.parse('10:00') | |
end2 = Time.parse('18:30') | |
timespan2 = (start2.to_i..(end2.to_i - 1)) | |
timespan2b = (start2.to_i..(end2.to_i - 1)).step(1.minute) | |
start3 = Time.parse('18:00') | |
end3 = Time.parse('23:45') | |
timespan3 = (start3.to_i..(end3.to_i - 1)) | |
timespan3b = (start3.to_i..(end3.to_i - 1)).step(1.minute) | |
t1a = Time.now | |
resulta = !( timespan1.to_a & timespan2.to_a ).empty? | |
t2a = Time.now | |
t1c = Time.now | |
resultc = !( timespan1b.to_a & timespan2b.to_a ).empty? | |
t2c = Time.now | |
t1b = Time.now | |
resultb = !( timespan2.to_a & timespan3.to_a ).empty? | |
t2b = Time.now | |
t1d = Time.now | |
resultd = !( timespan2b.to_a & timespan3b.to_a ).empty? | |
t2d = Time.now | |
puts "#{resulta} #{sprintf "%.3f", (t2a - t1a) * 1000.0} ms - timespan1 & timespan2" | |
puts "#{resultc} #{sprintf "%.3f", (t2c - t1c) * 1000.0} ms - timespan1 & timespan2" | |
puts "#{resultb} #{sprintf "%.3f", (t2b - t1b) * 1000.0} ms - timespan2 & timespan3" | |
puts "#{resultd} #{sprintf "%.3f", (t2d - t1d) * 1000.0} ms - timespan2 & timespan3" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment