473,813 Members | 2,875 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ArrayList.Conta ins() slower in NET 2.0 ?

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.Collecti ons.ArrayList list = new System.Collecti ons.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.Tic kCount;
//Find the object 1111 times in the collection
for (int j=0;j<1111;j++)
{
bool objectInList = list.Contains(o bjectToFind);
}
System.Console. WriteLine("Cont ains 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

Sep 11 '06 #1
12 5133
Robert Hooker <rh*****@noemai l.noemailwrote:
We are moving to .NET2.0 - and have noticed that .Contains() seems to be
around 20% slower under NET2, compared to NET1.1.
<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?
I would suggest avoiding using ArrayList.Conta ins 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.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Sep 12 '06 #2
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.Environm ent::get_TickCo unt()
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.Collecti ons.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.Environm ent::get_TickCo unt()
========VS 2005/.NET 2.0============ =
int32 [mscorlib]System.Environm ent::get_TickCo unt()
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.Collecti ons.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$0000
IL_005f: ldloc.s CS$4$0000
IL_0061: brtrue.s IL_0043
IL_0063: ldstr "Contains took "
IL_0068: call int32 [mscorlib]System.Environm ent::get_TickCo unt()

=============== =============== =============== ====

Based on the IL reference, these IL instructions are setting and loading
data into local variables and also do some judgement on the value.

Therefore, the 2.0 code actually sacrifice some performance to improve the
strength of such weak-type collection's methods. Also, based on my
experience, there're many such adjustment in the base class library of .net
framework which improve the code strength and type-safety with some
sacrifice on the performance. However, these performance hit is really
worthy from a higher layer perspective(suc h as from a whole application's
designing viewpoint).

Please feel free to let me know if there is any other information you
wonder.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead

=============== =============== =============== =====

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.

=============== =============== =============== =====

This posting is provided "AS IS" with no warranties, and confers no rights.




Sep 12 '06 #3
Hello Robert,

Have you got any further ideas on this issue or does the information in my
last reply helps you a little? If there is anything else we can help or any
other information you wonder, please feel free to let me know.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead
This posting is provided "AS IS" with no warranties, and confers no rights.

Sep 14 '06 #4
Looks like about the only change is an additional null test in the loop.
Hard to believe a single test would account for 20%, but maybe.
I think this fixes a bug so it is needed code.

--
William Stacey [MVP]

"Robert Hooker" <rh*****@noemai l.noemailwrote in message
news:ee******** ******@TK2MSFTN GP05.phx.gbl...
| 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.Collecti ons.ArrayList list = new System.Collecti ons.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.Tic kCount;
| //Find the object 1111 times in the collection
| for (int j=0;j<1111;j++)
| {
| bool objectInList = list.Contains(o bjectToFind);
| }
| System.Console. WriteLine("Cont ains 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
|
|
|
Sep 14 '06 #5
I believe the default comparison is also a bit slower.

Cheers,

Greg
"William Stacey [MVP]" <wi************ @gmail.comwrote in message
news:%2******** ********@TK2MSF TNGP02.phx.gbl. ..
Looks like about the only change is an additional null test in the loop.
Hard to believe a single test would account for 20%, but maybe.
I think this fixes a bug so it is needed code.

--
William Stacey [MVP]

"Robert Hooker" <rh*****@noemai l.noemailwrote in message
news:ee******** ******@TK2MSFTN GP05.phx.gbl...
| 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.Collecti ons.ArrayList list = new
System.Collecti ons.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.Tic kCount;
| //Find the object 1111 times in the collection
| for (int j=0;j<1111;j++)
| {
| bool objectInList = list.Contains(o bjectToFind);
| }
| System.Console. WriteLine("Cont ains 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
|
|
|


Sep 14 '06 #6
Thanks all - for you replies. I'm happy enough that the 15%-20% slow-down is
at least experienced by others and its not me on crack.

Collectively, your replies have given me a few hints as to ways to improve
things. (Avoid .Contains were we can, and where we can't, I'll investigate
other Comparers)

Thanks again
Rob

"Robert Hooker" <rh*****@noemai l.noemailwrote in message
news:ee******** ******@TK2MSFTN GP05.phx.gbl...
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.Collecti ons.ArrayList list = new System.Collecti ons.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.Tic kCount;
//Find the object 1111 times in the collection
for (int j=0;j<1111;j++)
{
bool objectInList = list.Contains(o bjectToFind);
}
System.Console. WriteLine("Cont ains 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

Sep 14 '06 #7
Hi Greg. "default comparison"? In the code, the only difference I see is 1
line with and added test for null.

--
William Stacey [MVP]

"Greg Young" <dr************ *******@hotmail .comwrote in message
news:%2******** ********@TK2MSF TNGP06.phx.gbl. ..
|I believe the default comparison is also a bit slower.
|
| Cheers,
Sep 14 '06 #8
Rob, have you tried substituting List<Tfor ArrayList? Perhaps the
implementation of that collection is a bit faster.

On Thu, 14 Sep 2006 10:09:12 -0600, "Robert Hooker"
<rh*****@noemai l.noemailwrote:
>Thanks all - for you replies. I'm happy enough that the 15%-20% slow-down is
at least experienced by others and its not me on crack.

Collectively , your replies have given me a few hints as to ways to improve
things. (Avoid .Contains were we can, and where we can't, I'll investigate
other Comparers)

Thanks again
Rob
--
http://www.kynosarges.de
Sep 14 '06 #9
Hello Chris,

Based on my check, the Generic List<Twill also suffer such an performance
overhead since it will also perform additional typesafe and null value
checking. Anyway, I think it is worthy of such overhead which may prevent
many potential problems from an application's perspective.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead
This posting is provided "AS IS" with no warranties, and confers no rights.

Sep 15 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
6215
by: Tristan | last post by:
Hi, My code reads through a file of shapes adding each one to an ArrayList as it does so. The problem is that for files with large numbers of shapes, calling ArrayList.Add(MyShape) is exceptionally slow. There is no way of knowing how many shapes need to
9
1416
by: Leon | last post by:
I have a webform in which when the user press generate button the form generate six unique ramdon numbers, the user can also type these six numbers in manually in any order. however, the user can also press a post button that post these six numbers into the database. My problem is that these six numbers need to be posted to the database from less to greatest, and all of my code is within the business tier class not the code behind class. ...
20
5990
by: Dennis | last post by:
I use the following code for a strongly typed arraylist and it works great. However, I was wondering if this is the proper way to do it. I realize that if I want to implement sorting of the arraylist then I have to handle this with a sort method that uses comparer. I can reference the properties of the Arraylist directly such as dim mylist as new FrameList mylist.Add(new FrameStructure) mylist(0).first = "blabla..." mylist(0).second...
48
4495
by: Alex Chudnovsky | last post by:
I have come across with what appears to be a significant performance bug in ..NET 2.0 ArrayList.Sort method when compared with Array.Sort on the same data. Same data on the same CPU gets sorted a lot faster with both methods using .NET 1.1, that's why I am pretty sure its a (rather serious) bug. Below you can find C# test case that should allow you to reproduce this error, to run it you will need to put 2 data files into current directory...
43
7043
by: tshad | last post by:
Which is better to use with an ArrayList: BinarySearch or Contains? The list is only going to have strings in it and it will be sorted. Thanks, Tom
14
1959
by: Kym | last post by:
Hey, I have an arraylist which I have added a number of objects to (all the same type of object), I need to be able to check if the array already contain the object before adding it, I have tried something like... if Not myArray.contains(newObject) then myArray.Add(newObject) end if This will work with a string object but not a user defined object.
3
2634
by: Christopher H | last post by:
I've been reading about how C# passes ArrayLists as reference and Structs as value, but I still can't get my program to work like I want it to. Simple example: code:------------------------------------------------------------------------------ class Program { static public ArrayList MyArrayList = new ArrayList();
44
39198
by: Zytan | last post by:
The docs for List say "The List class is the generic equivalent of the ArrayList class." Since List<is strongly typed, and ArrayList has no type (is that called weakly typed?), I would assume List<is far better. So, why do people use ArrayList so often? Am I missing somehing? What's the difference between them? Zytan
8
2596
by: Guy | last post by:
Is there a better way to search identical elements in a sorted array list than the following: iIndex = Array.BinarySearch( m_Array, 0, m_Array.Count, aSearchedObject ); aFoundObject= m_Array; m_ResultArray.Add ( aFoundObject);
0
9734
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10667
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10407
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9222
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7681
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5568
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5705
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3885
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3030
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.