队列< T> VS名单< T> [英] Queue<T> vs List<T>

查看:226
本文介绍了队列< T> VS名单< T>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前使用的是名单,其中,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.

这篇关于队列&LT; T&GT; VS名单&LT; T&GT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆