我想对ArrayList(System.Collections - C#)在开始时插入项目的速度进行性能测试。性能测试ArrayList in 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](http://www.red-gate.com/products/dotnet-development/ants-performance-profiler/)并查看性能正在发生的变化成。 –
为什么要测量'ArrayList'。从.NET 2.0开始已经被弃用了。 –
@亨克抱歉,您能向我解释为什么会出现这种情况吗?秒表仅包围插入 - 而不是其他任何东西。 – BigBug