我需要编写一个程序,它将获得大量的输入整数,大约10 ^ 6个数字,并以相同的顺序处理它们。 迭代中哪个数据结构最快..? 该程序必须在3秒内运行。
I need to write a program which will get very large number of input integers, about 10^6 numbers and process them in the same order. Which data structure will be the fastest in iteration..? The program must run within 3 seconds.
最满意答案
对于相同顺序的迭代,如果性能是一个关键问题,你可以使用一个简单的数组 (但它不允许动态增长数组而不需要额外的努力,所以你通常应该知道数组的最终大小才能使用它)。
您还可以使用List接口。 LinkedList和ArrayList支持有效的迭代和追加。 这样可以更好地维护代码,但性能稍差。
For iteration in the same order you can use a simple array if performance is a critical issue (but it does not allow growing the array dynamically without some extra effort, so you should usually be aware of the final size of the array in order to use it).
You can also use a List interface. Both LinkedList and ArrayList support efficient iterating and appending. This allows better maintainability for your code, but suffers from slightly worse performances.
更多推荐
发布评论