c# - 10000000个整数相加时为什么在ArrayList和List中赋值时间相同?

标签 c# list arraylist collections types

我有以下代码,其中我通过添加 10000000 个整数来比较 ArrayList 和 List 的性能。在 ArrayList 中,我有装箱和拆箱,所以在第一个循环中,添加整数的时间比在 List 中多得多,但是稍后分配整数到元素形式列表和 arrayList 的时间是相似的,即使在 arrayLIst 中我必须做拆箱。为什么?是因为 ArrayList 中整数的装箱和拆箱发生在第二个循环之前吗?

int numbers = 10000000;
ArrayList integerArrayList = new ArrayList();
Stopwatch timer = new Stopwatch();
timer.Start();

for (int i = 0; i < numbers; i++)
{
    integerArrayList.Add(i); //niejawna konwersja na object
}
timer.Stop();
var timeTestingIntegerArrayList = timer.ElapsedMilliseconds;
Console.WriteLine($"The time for adding {numbers} numbers to Arraylist is: {timeTestingIntegerArrayList} ms");
timer.Reset();

timer.Start();

for (int i = 0; i < numbers; i++)
{

    int number = (int)integerArrayList[i]; //unboxing
}
timer.Stop();
var arrayListWriteTime = timer.ElapsedMilliseconds;
Console.WriteLine($"The time for writting {numbers} numbers from Arraylist is: {arrayListWriteTime} ms");
timer.Reset();

Console.WriteLine($"The whole time for adding and writting {numbers} numbers from Arraylist is: {arrayListWriteTime + timeTestingIntegerArrayList} ms");` 

int numbers = 10000000;

List<int> integerList = new List<int>();
Stopwatch timer = new Stopwatch();

timer.Start();
for (int i = 0; i < numbers; i++)
{
    integerList.Add(i);
}
timer.Stop();   
var timeTestingIntegerList = timer.ElapsedMilliseconds;
Console.WriteLine($"The time for adding {numbers} numbers to list is: {timeTestingIntegerList} ms");
timer.Reset();

timer.Start();
for (var i = 0; i < numbers; i++)
{

    int number = integerList[i];

}
timer.Stop();
var listWriteTime = timer.ElapsedMilliseconds;
Console.WriteLine($"The time for assigning {numbers} numbers from list is: {listWriteTime} ms");
timer.Reset();


Console.WriteLine($"The whole time for adding and writting the integer numbers is: {timeTestingIntegerList + listWriteTime}");

最佳答案

你的程序产生相似的输出

The time for adding 10000000 numbers to Arraylist is: 1824 ms
The time for writting 10000000 numbers from Arraylist is: 100 ms
The whole time for adding and writting 10000000 numbers from Arraylist is: 1924 ms
The time for adding 10000000 numbers to list is: 111 ms
The time for assigning 10000000 numbers from list is: 77 ms
The whole time for adding and writting the integer numbers is: 188

but the time of assignig later the integer number to elements form list and arrayList is similar even though in arrayLIst I have to do unboxing

不,列表赋值比数组列表赋值相对快。如果比较 IL 生成的代码,ArrayList 和 List 赋值之间的区别几乎只是一条指令。

数组列表

L_0083: ldloc.s num8
    L_0085: callvirt instance object [mscorlib]System.Collections.ArrayList::get_Item(int32)
    L_008a: unbox.any int32
    L_008f: stloc.s num9
    L_0091: nop 
    L_0092: ldloc.s num8
    L_0094: stloc.s num7
    L_0096: ldloc.s num7
    L_0098: ldc.i4.1 
    L_0099: add 
    L_009a: stloc.s num8
    L_009c: ldloc.s num8
    L_009e: ldloc.0 

列表

L_017d: callvirt instance !0 [mscorlib]System.Collections.Generic.List`1<int32>::get_Item(int32)
L_0182: stloc.s num12
L_0184: nop 
L_0185: ldloc.s num11
L_0187: stloc.s num7
L_0189: ldloc.s num7
L_018b: ldc.i4.1 
L_018c: add 
L_018d: stloc.s num11
L_018f: ldloc.s num11
L_0191: ldloc.0 

L_008a:unbox.any int32 是数组列表中出现的唯一一条指令,因此与普通列表相比,数组列表中有一些额外的时钟滴答。 因此,正如预期的那样,由于拆箱,arraylist 比列表慢一点,但是与装箱相比,拆箱更快。

Is it because boxing and unboxing of integers in ArrayList occurs before second loop?

NO!!。两个循环彼此独立,没有共享变量或实例会导致您的假设。

关于c# - 10000000个整数相加时为什么在ArrayList和List中赋值时间相同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48721651/

相关文章:

c# - 将任何带有变音符号的传入 URL 重定向为等效的没有变音符号的 URL

c#英国邮政编码拆分

c# - Docker:dotnet ef数据库更新失败

python - 使用字典查找文件中单词出现的行号

c# - 如何清理 TreeView 中具有相同 Text 属性的 "TreeNode"s?

java - ArrayList 中字符串的唯一实例

c# - 为最短路径优化由 WPF 中的单元格组成的网格

list - append/3 的具体谓词变体的冗余答案

c# - 从存储过程结果集中填充 ArrayList

java - java中数组列表的合并排序