bokmann/business_time — pull request #44 by vivrass (2013-08-20 14:33:03+00:00)
Title: Optimization

Hi,
While using this library, I got a big problem for the time it takes to execute. I need to manage big chunk of business_time in the future. So, I took the time to optimize the "after" method.

The change: Directly jump (without business_time) the number of weeks needed in the future and I use BusinessDays in BusinessHours. This limits the BusinessHours.@hours loop to 40 and BusinessDays.@days loop at 5. This means the time needed to get any date in the future is almost constant.

On Ruby 1.9.3, here are my results (from 1 hours to 1 year)
# 0.6.2
n = 100; Benchmark.bm { |x| [1, 5, 10, 40, 160, 480, 1000, 2000].each {|hours| x.report("#{hours}:") { n.times { hours.business_hours.a>
user system total real
1: 0.110000 0.000000 0.110000 ( 0.109429)
5: 0.120000 0.000000 0.120000 ( 0.121477)
10: 0.190000 0.000000 0.190000 ( 0.185267)
40: 0.710000 0.010000 0.720000 ( 0.720219)
160: 2.570000 0.000000 2.570000 ( 2.573678)
480: 7.590000 0.020000 7.610000 ( 7.612694)
1000: 16.010000 0.030000 16.040000 ( 16.037597)
2000: 31.520000 0.060000 31.580000 ( 31.572364)

# My optimization
n = 100; Benchmark.bm { |x| [1, 5, 10, 40, 160, 480, 1000, 2000].each {|hours| x.report("#{hours}:") { n.times { hours.business_hours.after(Time.now) } } >
user system total real
1: 0.150000 0.000000 0.150000 ( 0.153765)
5: 0.200000 0.000000 0.200000 ( 0.207473)
10: 0.340000 0.010000 0.350000 ( 0.343515)
40: 0.130000 0.000000 0.130000 ( 0.132431)
160: 0.130000 0.000000 0.130000 ( 0.131817)
480: 0.130000 0.000000 0.130000 ( 0.130287)
1000: 0.130000 0.000000 0.130000 ( 0.130852)
2000: 0.140000 0.000000 0.140000 ( 0.137180)

This is about 200 times faster for 1 business_year in the future.

So, here is my questions for you
1. Are you interested in this optimization?
2. What would you like me to fix in term of nomenclature?
3. Do you see any potential bugs?

Any comments are appreciated. If you are interested in this fix, I'll do the same things for the "before" methods.