c# - Data Structure for caching (Key = DateTime, Value = decimal) -
i have function calculates employee hours in day. but, it's slow because considers carry hours , other things.
i figure can optimize caching hours in day , update on change.
i'm looking can do:
set(date,hours) hashoursfor(date) gethoursfor(date)
what should use this?
i use dictionary
, wondering if there not sort of hash way set, get, , contains quickly.
you use dictionary<datetime,int>
, cache hours against date, so:
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);
i normalise date that's date , doesn't care time part, otherwise throw off comparison. i'm assuming have whole hours otherwise might want change int
double
etc.
Comments
Post a Comment