Jump to content

Okay I was watching the latest episode on collections but for the life of me I cannot understand ...


G+_abby Sand
 Share

Recommended Posts

Different collections use different methods to access data, and so have different properties like memory use and efficiency. If, for example, you have 1000 records that you want to look up by name, if you just have them in a list you'll have to search through all of them comparing names to find one every time you do a lookup. That's an average of 500 comparisons per lookup, dreadfully slow. If you keep them sorted, you can use a binary search and average 10 comparisons. But a dictionary uses a hash function to access members with fewer than 2 comparisons on average, so looking up by name is very efficient (but there's a slight catch: the dictionary takes more memory than a list. Such tradeoffs are common in choosing data structures and algorithms).

Link to comment
Share on other sites

 Share

×
×
  • Create New...