c# - Dictionary vs SortedList for data storage and processing -
c# - Dictionary vs SortedList for data storage and processing -
i have created dictionary, consisting of datetime key , custom class value.
class class2 { public decimal value1 { get; set; } public decimal value2 { get; set; } public decimal value3 { get; set; } } class testclass { public static void main(string[] args) { var dict = new dictionary<datetime, class2>(); dict.add(new datetime(2014, 6, 1), new class2() { value1 = 1, value2 = 2, value3 = 3 }); dict.add(new datetime(2014, 6, 2), new class2() { value1 = 4, value2 = 5, value3 = 6 }); dict.add(new datetime(2014, 6, 3), new class2() { value1 = 10, value2 = 20, value3 = 40 }); dict.add(new datetime(2014, 6, 4), new class2() { value1 = -5, value2 = -6, value3 = -8 }); // form list consisting of value1 , pass list function processing } } in comment line, want select particular info points value1 , form list. these info points selection criteria based on time (that why need datetime field). list passed function farther processing.
my question two-fold:
should utilize dictionary store kind of data? have seen other posts, , talking utilize of sortedlist. info going add together these container classes in sequential order (of time).
what's best practice creating list (of value1)? have created separate list scratch, know if there improve alternative other (given have stored in info in dictionary instance).
use sorteddictionary<tkey, tvalue> class.
sorteddictionary<datetime, class2> dict = new sorteddictionary<datetime, class2>(); following documentation explains difference between using sortedlist vs sorteddictionary
the sorteddictionary<tkey, tvalue> generic class binary search tree o(log n) retrieval, n number of elements in dictionary. in respect, similar sortedlist<tkey, tvalue> generic class. 2 classes have similar object models, , both have o(log n) retrieval. 2 classes differ in memory utilize , speed of insertion , removal:
sortedlist<tkey, tvalue> uses less memory sorteddictionary<tkey, tvalue>.
sorteddictionary<tkey, tvalue> has faster insertion , removal operations unsorted data: o(log n) opposed o(n) sortedlist<tkey, tvalue>.
if list populated @ 1 time sorted data, sortedlist<tkey, tvalue> faster sorteddictionary<tkey, tvalue>.
for question:
what's best practice creating list (of value1)?
list<decimal> list = dict.values.select(r=> r.value1).tolist(); for comment:
how form list consisting of values1, say, lastly 3 days
list<decimal> listofvalue1 = dict.where(r=> r.key >= datetime.today.adddays(-3) && r.key <= datetime.today) .select(r=> r.value.value1) .tolist(); c# dictionary sortedlist
Comments
Post a Comment