Optimizing date calculations

I wrote a naive little algorithm to calculate the end date of a scheduled event.

There are the following restraints:

  1. Total number of hours
  2. Number of hours per day
  3. Workdays per week
  4. Skip holidays

So, the pseudo-code looks like this:
While there are hours
    If it's a workday
         diminish the hour counter by the amount of hours per day
    Go to the next day

This works, but it's slow. Let's do this moar better!

New pseudo-code:
Hours / Hours per day                   ===> Days
Days / Workdays per week            ===> Weeks
(7 - workdays per week) * Weeks ===> More Days 
Add the number of days to the initial date to get the end date. 
Calculate the number of holidays in the range ===> Days 
While there are days
    If it's a workday
         diminish the day counter by one
    Move the end date on day forward.
This improved algorithm is 150 times faster than the original for 630 hours, 5 hours per day (or 126 days)

Comments

Popular posts from this blog

A recipe for failure

Ubuntu - Auto-mount an encrypted drive

Spaghetti code