-1

I have a function which calculates employee hours in a day. But, it's slow because it considers carry hours and other things.

I figure I can optimize this by caching the hours in a day and only update on change.

I'm looking for something that can quickly do:

Set(date,hours)
HasHoursFor(date)
GetHoursFor(date)

What should I use for this?

I could use a Dictionary, but I am wondering if there is not some sort of hash way to set, get, and contains quickly.

4

1 回答 1

1

您可以使用 aDictionary<DateTime,int>并根据日期缓存小时数,如下所示:

Dictionary<DateTime,int> dict = new Dictionary<DateTime,int>();

void Set(DateTime date, int hours)
{
    if (dict.Contains(date)) dict.Remove(date);

    dict.Add(date.Date,hours);
}

bool HasHoursForDate(DateTime date)
{
    return dict.Contains(date.Date);
}

int GetHoursForDate(DateTime date)
{
    return dict[date.Date];
}

Set(DateTime.Now,8);

我将日期标准化,因此它只是日期,而不关心时间部分,否则会导致比较失败。我还假设您有整个小时,否则您可能想要更改intdouble等。

于 2013-04-18T13:38:34.000 回答