在.NET 2.0中,ArrayList.Contains()速度较慢? [英] ArrayList.Contains() slower in NET 2.0 ?

查看:76
本文介绍了在.NET 2.0中,ArrayList.Contains()速度较慢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在转向.NET2.0 - 并注意到.Contains()似乎比NET1.1慢了大约20%,与NET1.1相比。


此代码显示问题:

static void Main(string [] args)

{

System.Collections。 ArrayList list = new System.Collections.ArrayList();


//设置代码

//

//添加10000对象集合。

//对于这个

演示而言,不在乎对象是什么类型

for(int i = 0; i <100000; i ++)

{

list.Add(new EventArgs());

}

//我们将找到这个对象(一直在

列表的末尾)

object objectToFind = list [99998];

//

//结束设置代码


//时间码

//

long startT = Environment.TickCount;

//在集合中查找对象1111次

for(int j = 0; j< 1111; j ++)

{

bool objectInList = list.Contains(objectToFind);

}

System.Console。 WriteLine(" Contains take" +(Environment.TickCount-startT)+"

(ms)");

System.Console.ReadLine();

}


在NET1.1下,我一直比NET2下的相同代码的时间快20%左右。 。


有没有人注意到这个?有没有办法可以避免任何新的代码

正在框架的内部执行?

谢谢

Rob

We are moving to .NET2.0 - and have noticed that .Contains() seems to be
around 20% slower under NET2, compared to NET1.1.

This code shows the issue:
static void Main(string[] args)
{
System.Collections.ArrayList list = new System.Collections.ArrayList();

//Setup code
//
//Add 10000 objects to a collection.
//Don''t care what type the object is for the purposes of this
demonstration
for (int i=0; i<100000; i++)
{
list.Add(new EventArgs());
}
//We are going to find this object (intentially right at the end of the
list)
object objectToFind = list[99998];
//
// End setup code

// Timing code
//
long startT = Environment.TickCount;
//Find the object 1111 times in the collection
for (int j=0;j<1111;j++)
{
bool objectInList = list.Contains(objectToFind);
}
System.Console.WriteLine("Contains took "+(Environment.TickCount-startT)+"
(ms)");
System.Console.ReadLine();
}

Under NET1.1 I consistently get times in the order of 20% quicker than the
same code under NET2.

Has anyone else noticed this? Is there a way I can avoid whatever new code
is being executed in the bowels of the framework?
Thanks
Rob

推荐答案

Robert Hooker< rh ***** @ noemail.noemailwrote:
Robert Hooker <rh*****@noemail.noemailwrote:

我们正在转向.NET2.0 - 并注意到.Contains()似乎比NET1.1慢了大约20%,与NET1.1相比。
We are moving to .NET2.0 - and have noticed that .Contains() seems to be
around 20% slower under NET2, compared to NET1.1.



< snip>

<snip>


有没有人注意到这个?有没有办法可以避免任何新的代码

正在框架的内部执行?
Has anyone else noticed this? Is there a way I can avoid whatever new code
is being executed in the bowels of the framework?



我建议避免在相当大的
列表中使用ArrayList.Contains。使用具有常量值(或值等于

各自键)的哈希表,检查包含的速度可能远远快于

列表。 (你可以随时保留一份清单*如果你需要订购

等)


-

Jon Skeet - < sk *** @ pobox.com>
http:// www .pobox.com / ~silet 博客: http://www.msmvps .com / jon.skeet

如果回复小组,请不要给我发邮件

I would suggest avoiding using ArrayList.Contains on pretty large
lists. Using a hashtable with a constant value (or a value equal to the
respective key) is likely to be far faster for checking inclusion than
a list. (You can always keep a list *as well* if you need ordering
etc.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


Hello Robert,


至于ArrayList包含方法性能问题,我刚刚根据你提供的代码进行了一些测试并且确实遇到了相同的

行为。(通过控制台应用程序测试).net 1.1版本比.net 2.0版本快了约44美元。


比较生成了两个版本的IL代码,.net 2.0一个具有

在包含内的一些附加指令。在
for循环中的ArrayList方法。这是我得到的disassemby代码:


========== VS 2003 / .NET 1.1 ===========
IL_002e:调用int32 [mscorlib] System.Environment :: get_TickCount()

IL_0033:conv.i8

IL_0034:stloc.3

IL_0035:ldc.i4.0

IL_0036:stloc.sj

IL_0038:br.s IL_0049

IL_003a:ldloc。 0

IL_003b:ldloc.2

IL_003c:callvirt实例bool

[mscorlib] System.Collections.ArrayList :: Contains(object)

IL_0041:stloc.s objectInList

IL_0043:ldloc.sj

IL_0045:ldc.i4.1

IL_0046 :add

IL_0047:stloc.sj

IL_0049:ldloc.sj

IL_004b:ldc.i4 0x457

IL_0050:blt.s IL_003a

IL_0052:ldstr包含花了

IL_0057:调用int32 [mscorlib] System.Environment :: get_TickCount()

======== VS 2005 / .NET 2.0 =============

int32 [ mscorlib] System.Environment :: get_TickCount()

IL_003c:conv.i8

IL_003d:stloc.3

IL_003e:ldc.i4。 0

IL_003f:stloc.sj

IL_0041:br.s IL_0054

IL_0043:nop

IL_0044:ldloc .0

IL_0045:ldloc.2

IL_0046:callvirt实例bool

[mscorlib] System.Collections.ArrayList :: Contains(object)

IL_004b:stloc.s objectInList

IL_004d:nop

IL_004e:ldloc.sj

IL_0050:ldc。 i4.1

IL_0051:添加

IL_0052:stloc.sj

IL_0054:ldloc.sj

IL_0056: ldc.i4 0x457

IL_005b:clt

IL_005d:stloc.s CS
Hello Robert,

As for the ArrayList Contains method performance issue, I''ve just performed
some tests according to the code you provided and did encounter the same
behavior.(test through console applcations) The .net 1.1 version is about
14% faster than .net 2.0 version.

Comparing the generated IL code of the two version, the .net 2.0 one has
some additional instructions within the "contains" method of ArrayList in
the for loop. Here are the disassemby code I got:

==========VS 2003/.NET 1.1===========
IL_002e: call int32 [mscorlib]System.Environment::get_TickCount()
IL_0033: conv.i8
IL_0034: stloc.3
IL_0035: ldc.i4.0
IL_0036: stloc.s j
IL_0038: br.s IL_0049
IL_003a: ldloc.0
IL_003b: ldloc.2
IL_003c: callvirt instance bool
[mscorlib]System.Collections.ArrayList::Contains(object)
IL_0041: stloc.s objectInList
IL_0043: ldloc.s j
IL_0045: ldc.i4.1
IL_0046: add
IL_0047: stloc.s j
IL_0049: ldloc.s j
IL_004b: ldc.i4 0x457
IL_0050: blt.s IL_003a
IL_0052: ldstr "Contains took "
IL_0057: call int32 [mscorlib]System.Environment::get_TickCount()
========VS 2005/.NET 2.0=============
int32 [mscorlib]System.Environment::get_TickCount()
IL_003c: conv.i8
IL_003d: stloc.3
IL_003e: ldc.i4.0
IL_003f: stloc.s j
IL_0041: br.s IL_0054
IL_0043: nop
IL_0044: ldloc.0
IL_0045: ldloc.2
IL_0046: callvirt instance bool
[mscorlib]System.Collections.ArrayList::Contains(object)
IL_004b: stloc.s objectInList
IL_004d: nop
IL_004e: ldloc.s j
IL_0050: ldc.i4.1
IL_0051: add
IL_0052: stloc.s j
IL_0054: ldloc.s j
IL_0056: ldc.i4 0x457
IL_005b: clt
IL_005d: stloc.s CS


4


这篇关于在.NET 2.0中,ArrayList.Contains()速度较慢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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