DateTimeExtensions icon indicating copy to clipboard operation
DateTimeExtensions copied to clipboard

Improve performance of AddWorkingDays algorithm

Open astiskala opened this issue 10 years ago • 1 comments

Hi There,

Firstly, thanks for the great library.

I know it is noted that the AddWorkingDays extension method is not designed to be used with large datasets. Is there any possibility of improving the performance of this method so that it can be run more efficiently?

I'm happy to have a go at this myself, my thoughts are to create a in-memory map of:

  1. The date component of the DateTime
  2. The integer of the number of days to add
  3. The resultant date

Then, each time the method is run, first check if the date-integer combination is in the map. If so, get the resultant date and add on the time component.

This method is making a few assumptions about how working days are calculated, so I'd like to hear your thoughts on this.

Adam

astiskala avatar Feb 17 '16 23:02 astiskala

Hi,

Well, the worst part of the calculation is actually translate the working days, into calendar days. Is short, it's only your step2. That's because we need to skip holidays and weekends.

For this to work in a not iterative algorithm, we need to calculate the number of weekends, and holidays not weekends, in the time period. Then subtract that value from the virtual end date (date as in just calculating calendar days = DateTime.AddDays(n)

joaomatossilva avatar Feb 18 '16 11:02 joaomatossilva