【发布时间】:2011-10-23 06:58:56
【问题描述】:
我想对 ArrayList(System.Collections - C#) 在开头插入项目的速度进行性能测试。
我打开了一个文件来读取数据行,设置了一个秒表,还创建了一个 ArrayList 来添加项目(如下):
Stopwatch watchTime = new Stopwatch();
Double totalTime = 0;
using (StreamReader readText = new StreamReader("data.txt"))
{
String line;
Int32 counter = 0;
while ((line = readText.ReadLine()) != null)
{
}
}
我使用计数器来跟踪我要进入 ArrayList 的项目数。
在 while 循环中,我有以下内容:
watchTime.Start();
theList.Insert(0, line);
watchTime.Stop();
Double time = watchTime.Elapsed.TotalMilliseconds;
totalTime = totalTime + time;
Console.WriteLine(time);
watchTime.Reset();
++counter;
这是检查将项目插入到 ArrayList 开头的速度的正确方法吗?
我制作了另一个程序,它做同样的事情 - 但是使用字典。令我惊讶的是,这个 ArrayList 插入项目所需的时间比 Dictionary 所需的时间长得多。为什么会这样?
【问题讨论】:
-
您可以启动像 ANTS Profiler 这样的性能分析器并查看性能进展情况。
-
为什么要测量
ArrayList。自 .NET 2.0 以来,它已被弃用。 -
@Henk 对不起,你能解释一下为什么会这样吗?秒表仅围绕插入 - 没有其他任何东西。
标签: c# performance insert arraylist performance-testing