I am designing a program and I am looking to decide over a Dictionary vs a List. Since I know I have unique values per item I imagined a Dictionary/List that looks like:
Dictionary<(int k1, string k2, int k3 ), myDataClassForDic> myDict = ...;
return myDict[(target_key1, target_key2, target_key3)]
// vs.
List<myDataClassForList> myList;
foreach (v in myList)
{
if ((v.k1 == target_key1) && (v.k2 == target_key2) && (v.k3 == target_key3))
{
return v;
}
}
My Dictionary/List size range is 1~100, but more commonly on the lower end < 20. I have a decent amount of retrievals based on the dic keys (sometimes not ALL keys so I have loop through the dictionary sometimes to get all items). And I am also constantly adding and removing items (no replacements) from the dic/List staying within whatever its max size is for the situation (FIFO). With always a unique combination of k1, k2, k3.
I was told that Dictionary Adding/Removing has a costly overhead for the hashmap, more so than a List and whether I have considered that. I also did a bit of googling and Copilot asking and found that a Dic is typically O(1) and therefore better than a List, but I also have some incomplete lookups that become O(n) as I have to find my target(s). But also there isa hashmap use cost as 3 values must be mashed together to get my key and then value.
Lists are typically much lighter, faster to loop through. But otherwise I lose on O(1) on direct key lookups, and removals from the head (I have no adds to anywhere but the end).
Copilot says the the memory overhead cost is 50%~100% more than a List and CPU overhead cost is 10%~20% more, but offset by O(1) complexity. 50~100% sounds like ALOT, which tilts me to List.
My usecase also means I probably will have lots of these Lists/Dicts of smaller sizes than I do bigger sizes. As I am tracking something that moves through various processes and when it finishes I output a log and delete the tracked data.
If I have loads of smaller sizes and only some larger sizes will the List be better for BOTH memory and Speed/CPU costs? Or is the extra memory cost neglible for the Dict with the O(1) benefit as well as the ability to much more easily handle sizes of up to 100 items?