Array vs. ArrayList

  • Thread starter Thread starter Bob Rundle
  • Start date Start date
I found the test a little bit flawed. But instead of just complaining I
decided to run my own tests. I forced garbage collection after each test,
otherwise the first test always got some advantange against the second.
I do some boxing and unboxing (not just adding). I didn't do a remove test,
'cause you cannot remove from System.Array.

using System;

namespace ConsoleApplication1
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
Console.WriteLine("Starting Tests...");
DateTime[] stamps = new DateTime[5];
Object obj;
Int32 obj_i;
Double diff;
const Int32 len = 10000000;
Console.WriteLine("Test Array...");
stamps[0] = DateTime.Now;
Object[] array = new Object[len];
for(Int32 i = 0; i < len; i++)
array = i;
for(Int32 i = 0; i < len; i++)
obj_i = (Int32) array;
for(Int32 i = 0; i < len; i++)
array = new Object();
for(Int32 i = 0; i < len; i++)
obj = array;
for(Int32 i = 0; i < len; i++)
array = null;
stamps[1] = DateTime.Now;
Console.WriteLine(stamps[0].Ticks.ToString("N"));
Console.WriteLine(stamps[1].Ticks.ToString("N"));
diff = stamps[1].Ticks - stamps[0].Ticks;
Console.WriteLine(Convert.ToDecimal(diff).ToString("N"));
Console.WriteLine(Convert.ToDecimal(diff/10000000.0).ToString("N"));
array = null;
System.GC.Collect();
Console.WriteLine("Test ArrayList...");
stamps[2] = DateTime.Now;
System.Collections.ArrayList arrayList = new
System.Collections.ArrayList(len);
for(Int32 i = 0; i < len; i++)
arrayList.Add(i);
for(Int32 i = 0; i < len; i++)
obj_i = (Int32) arrayList;
for(Int32 i = 0; i < len; i++)
arrayList = new Object();
for(Int32 i = 0; i < len; i++)
obj = arrayList;
for(Int32 i = 0; i < len; i++)
arrayList = null;
stamps[3] = DateTime.Now;
Console.WriteLine(stamps[2].Ticks.ToString("N"));
Console.WriteLine(stamps[3].Ticks.ToString("N"));
diff = stamps[3].Ticks - stamps[2].Ticks;
Console.WriteLine(Convert.ToDecimal(diff).ToString("N"));
Console.WriteLine(Convert.ToDecimal(diff/10000000.0).ToString("N"));
arrayList = null;
System.GC.Collect();
Console.ReadLine();
}
}
}


Output (With both DEBUG & TRACE disabled):
Starting Tests...
Test ArrayList...
632,370,678,527,642,077.00
632,370,678,634,673,327.00
107,031,250.00
10.70
Test Array...
632,370,678,636,548,327.00
632,370,678,731,392,077.00
94,843,750.00
9.48

I ran the tests about 25 times, got very stable responses (under 3% margins,
but usually both results vary the same way).

After disabling the "optimize code option" and enabling both TRACE and DEBUG
I got the following results:

Starting Tests...
Test ArrayList...
632,370,683,200,142,077.00
632,370,683,314,673,327.00
114,531,250.00
11.45
Test Array...
632,370,683,316,392,077.00
632,370,683,416,548,327.00
100,156,250.00
10.02

Maybe in your test you had no too much memory, and you were running the
ArrayList test AFTER the Array test. Could it be that the system was swapping
to disk?
 
Back
Top