队列< T> VS名单< T>队列、名单、GT、LT

2023-09-04 01:39:31 作者:从不低调@

我目前使用的是名单,其中,T> 为队列(使用 LST [0] 然后 lst.removeAt(0))持有的对象。有大约20个项目最多,在给定的时间。我意识到有一个实际的问答LT; T> 类。我不知道是否有任何好处(性能,内存等)使用问答LT; T> 通过名单,其中,T> 像一个队列?

I'm currently using a List<T> as a queue (use lst[0] then lst.removeAt(0)) to hold objects. There's about 20 items max at a given time. I realized there was an actual Queue<T> class. I'm wondering if there's any benefit (performance, memory, etc.) to using a Queue<T> over a List<T> acting like a queue?

推荐答案

性能可以成型。虽然如此少的项目这种情况下,您可能需要运行code百万次真正得到有价值的差异。

Performance can be profiled. Though in this case of so few items, you may need to run the code millions of times to actually get worthwhile differences.

我要说的是:问答LT; T&GT; 将暴露您的的的意图的更明确的人都知道如何队列工作。

I will say this: Queue<T> will expose your intent more explicitly, people know how a queue works.

在使用像队列中的列表不清晰,特别是如果你有很多不必要的索引和 RemoveAt(magicNumber) code。 出列是从一个code维修点多了很多易损件。

A list being used like a queue is not as clear, especially if you have a lot of needless indexing and RemoveAt(magicNumber) code. Dequeue is a lot more consumable from a code maintenance point of view.

如果这则让你可衡量的绩效问题,就可以解决这个问题。不要满足每一个的的潜力的性能问题放在首位。

If this then gives you measurable performance issues, you can address it. Don't address every potential performance issue upfront.