HashSet vs. List performance

前端 未结 12 2393
小鲜肉
小鲜肉 2020-11-22 09:19

It\'s clear that a search performance of the generic HashSet class is higher than of the generic List class. Just compare the has

12条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-22 09:39

    The answer, as always, is "It depends". I assume from the tags you're talking about C#.

    Your best bet is to determine

    1. A Set of data
    2. Usage requirements

    and write some test cases.

    It also depends on how you sort the list (if it's sorted at all), what kind of comparisons need to be made, how long the "Compare" operation takes for the particular object in the list, or even how you intend to use the collection.

    Generally, the best one to choose isn't so much based on the size of data you're working with, but rather how you intend to access it. Do you have each piece of data associated with a particular string, or other data? A hash based collection would probably be best. Is the order of the data you're storing important, or are you going to need to access all of the data at the same time? A regular list may be better then.

    Additional:

    Of course, my above comments assume 'performance' means data access. Something else to consider: what are you looking for when you say "performance"? Is performance individual value look up? Is it management of large (10000, 100000 or more) value sets? Is it the performance of filling the data structure with data? Removing data? Accessing individual bits of data? Replacing values? Iterating over the values? Memory usage? Data copying speed? For example, If you access data by a string value, but your main performance requirement is minimal memory usage, you might have conflicting design issues.

提交回复
热议问题