473,888 Members | 1,419 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
12 5139
Strangely enough - List<T"Contains " is measureably SLOWER (about 10%
slower) than ArrayList's "Contains".
Sigh...

"Chris Nahr" <ch************ @kynosarges.dew rote in message
news:5s******** *************** *********@4ax.c om...
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.

Collectivel y, 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 18 '06 #11
Sorry about that, I should have checked the implementations first...

The algorithms look identical in Reflector but List<T>.Contain s uses
an EqualityCompare r<Tto check for identical elements while
ArrayList.Conta ins calls the usual Equals method. I suppose that
might account for the difference.

On Mon, 18 Sep 2006 14:32:11 -0600, "Robert Hooker"
<rh*****@noemai l.noemailwrote:
>Strangely enough - List<T"Contains " is measureably SLOWER (about 10%
slower) than ArrayList's "Contains".
Sigh...
--
http://www.kynosarges.de
Sep 19 '06 #12

"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.
There is definitely an unneeded test for null in the second loop in
ArrayList.Conta ins(item)
Equals is required to be commutative
(http://msdn2.microsoft.com/en-us/library/ms173147.aspx), so one can easily
call item.Equals since item has already been found to be non-null, instead
of on each array element, especially since Equals is required to test its
argument for null anyway.

Furthermore, it seems that List<T>.FindInd ex(delegate (T x) { return
item.Equals(x); }) should be faster than List<T>.Contain s(item) for the same
reason.
>
--
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
|
|
|


Oct 26 '06 #13

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

Similar topics

2
6218
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
1418
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
5993
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
4504
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
7055
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
1969
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
2637
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
39214
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
2601
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
9800
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11181
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
10778
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
9597
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
7990
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
7148
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5819
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...
1
4642
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3252
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.