If the start date is normally the same, then you have two options.
1. Come up with a key scheme for start / end date combinations, and
hash the answers as you compute them. This is the most general scheme,
but its value varies with the cost of computing the hash key: the more
expensive the key, the less effective the cache. Try this:
public struct StartEndDateTime
{
private static Hashtable _cache = new Hashtable();
private DateTime _startDate;
private DateTime _endDate;
public StartEndDateTime(DateTime startDate, DateTime endDate)
{
this._startDate= startDate;
this._endDate= endDate;
}
public DateTime StartDate
{
get { return this._startDate; }
}
public DateTime EndDate
{
get { return this._endDate; }
}
public override int GetHashCode()
{
return this._startDate.GetHashCode() +
this._endDate.GetHashCode();
}
public override bool Equals(object obj)
{
if (obj is DateTime)
{
return Equals((DateTime)obj);
}
else
{
return false;
}
}
public bool Equals(DateTime otherDate)
{
return this._startDate.Equals(otherDate._startDate) &&
this._endDate.Equals(otherDate._endDate);
}
public long GetWorkingDayMinutes()
{
long result;
obj cacheResult = StartEndDateTime._cache[this];
if (cacheResult != null)
{
result = (long)cacheResult;
}
else
{
... do your math here ...
StartEndDateTime._cache[this] = result;
}
return result;
}
}
.... then you can use this struct in your application like this:
StartEndDateTime period = new StartEndDateTime(startDate, endDate);
long periodMinutes = period.GetWorkingDayMinutes();
.... and the fact that there's a cache involved is transparent. I would
try this first and then time it to see how performant it is.
2. At least cache the answer to your second part: minutes prior to
first full week, along with the start date from the previous request.
When you enter the routine, you can check the start date against the
previous start date and, if they're the same, use the previous "minutes
prior to first full week" and save yourself that calculation. You can
do the same with the end date. That saves you two of the three most
expensive calculations, leaving you only to calculate the full weeks
minutes each time (until the end date changes, then you have to do the
math again).
You could even come up with a hash table scheme for the end date ->
minutes after last full week answer, and see if the hashing is faster
than the mathematics.
Again, this way you still have to do some math, but you cut back
drastically on the amount you have to do. It's probably less performant
than a full start/end date -> final result caching scheme, but it will
be a big help, nonetheless.