1

I have a dictionary where the key is a string; while the values are a list of floats.

key = "Sensor1"; values = new List<float>{0.4, 0.5, 0.2, 0.4}; 

Is this the best data structure that I can use for my case? My concern is related to the time spent parsing the list of values (I will parse this often, both reading it than for write), but I don't see another way, beside a dictionary with the list in it for the values.

Edit

40 sensors read values 10 times a second; I read all these values and save in the dictionary the list with the values, and the key. There are cases where the values in the list for each sensor has to be replaced. The whole process is quite complex; this is the quickest way to summarize it; hopefully is clear.

5
  • 2
    If you mean parsing = enumerating, then both data structures are used for what they were intended in your sample, so probably yes... what is your objective? millions of elements, performance criticality? Commented Jun 5, 2016 at 10:41
  • 1
    "Is this the best data structure that I can use for my case?" Best for what? You haven't explained your case or how you want to use it at all. Commented Jun 5, 2016 at 10:41
  • Let me give an example: each sensor return N values saved in the list; the first pass of my application is reading these values at intervals, save them in a list, and then create the entry in the dictionary; and so on for each sensor. The second pass, call the key, read each value in the list, and replace the lowest value with a new reading from the sensor. This means that I read and write multiple times per each cycle. I do sample 10 times every second, on 40 sensors, so 400 samples per second. Commented Jun 5, 2016 at 10:48
  • From the use scenario looks like the important part in not the Dictionary, but the data structure holding the values per each key. I think the way you formulated the question is misleading and most of the answers will (and do) concentrate on the key lookup part. Commented Jun 5, 2016 at 10:58
  • @IvanStoev correct; I thought that the question was clear, since I am asking if this data structure is performant enough for a scenario where values will be read and written often. Commented Jun 5, 2016 at 11:01

4 Answers 4

1

It is generally Dictionary that is used mostly for such tasks if your data structure allows it, it is extremely fast since notation is O(1).

I would suggest looking into HashSet, it might be more efficient for your case.

Comparison between Dictionary, List and HashSet could be found here.

Sign up to request clarification or add additional context in comments.

1 Comment

True, although a dictionary is O(1), a list is O(n); so you take that as greatest vaue and the 1 change to constant and discarded. The time needed to search for a value in the list won't be 1 anymore, only the time to find the list related to the key passed.
0

If your list represents related values that share some common behavior, you may benefit from encapsulating this list in a class:

class SensorData { public IList<float> Values {get;} public SensorData(IEnumerable<float> values) { Values = values.ToList(); } // Add some useful methods that operate on Values here ... } 

Now instead of making

IDictionary<string,List<float>> 

you would make

IDictionary<string,SensorData> 

which could potentially provide for better readability of your code.

3 Comments

Interesting approach, it add in complexity, since I need another class, but I may add the logic related to how I read and modify the list values. BTW is there a particular reason for using a IList ? Thanks
@newbiez I prefer using an interface instead of class type for fields to avoid locking myself into a particular implementation of the collection. I do this initially, and very often it stays like that in the class. In rare cases when I need methods from List not available on IList (e.g. for adding a range of values) I change the type back to List.
@newbiez As far as additional complexity goes, it pays off very quickly with better readability when your list starts behaving like a single meaningful thing. The overhead in terms of additional memory is minimal, but readability impact is usually more important.
0

For the type of data that you are describing you need to use some kind of hash map, there is multiple ways of implementing a hash map (like c# dictionary), for example in each key you can save the value as a tree(binary , red/black,..), a linked list, sorted array and so on.. also need to pick a hash function that needs to be very fast. I would trust microsoft for implementing dictionary in the best way with suitible performences.

Comments

0

Not sure what you mean by "parsing the list of values" but probably you're looking for Lookup.

It is similar to Dictionary<TKey, Collection<TElement>>. You can group items by key and access collection by this key

Comments