R
Robert Hooker
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
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