Perf testing .net framework 2: generics

After installing .net framework 2.0 beta 1 a while ago, I've been wanting to perf test C# generics to see if the speed increase from untyped containers is noticeable. I'm surprised on the little effect they had on tests, but on the positive side, Whidbey framework seems considerably faster than 1.1 anyway.

Testing part 1 was done by compiling the following on both 2.0 and 1.1:

class MyClass : IComparable {
public readonly int x;
public MyClass(int x) { this.x = x; }
public int CompareTo(object o) {
return this.x.CompareTo(((MyClass)o).x);
}
}
static void Main(string[] args) {
ArrayList a = new ArrayList();
Random r = new Random();
for (int i = 0; i < 1000000; ++i)
a.Add(new MyClass(r.Next()));
a.Sort();
}

The code simply creates custom objects and sorts them. On framework 1.1, running the program took about 4.3 seconds (average of 10 repetitions). On framework 2.0, the exactly same source produced an executable with a running time of 2.7 seconds - that's a 37% improvement just by switching the framework version!

At this time, I was expecting quite a lot from generics. So I changed the code a bit:

class MyClass : IComparable<MyClass> {
public readonly int x;
public MyClass(int x) { this.x = x; }
public int CompareTo(MyClass o) {
return this.x.CompareTo(o.x);
}
public bool Equals(MyClass o) { return this.x == o.x; }
}
static void Main(string[] args) {
Random r = new Random();
List l = new List<MyClass>();
for (int i = 0; i < 1000000; ++i)
l.Add(new MyClass(r.Next()));
l.Sort();
}

The IComparable now uses a generic typed version, and I've replaced untyped ArrayList with the generic List type. And the runtime? 2.3 seconds. That's about 15% off from the 2.0 result - I wouldn't have been surprised by even more drastic figures. I still wanted to try reading the list through, so I added the following:

long sum = 0;
foreach (MyClass mc in a) { sum += mc.x; }

.net framework 1.1 used about 4.6 seconds; 2.0 did it in 2.9 secs and the generic version clocked 2.5 seconds. So, reading the array through in a foreach loop didn't really make a difference - the relative speed differences were equal.

One shouldn't be too disappointed on the performance of generics, though: The speed increase from 1.1 to generics version is a whopping 46% - the fact that even untyped containers got quite a speedup doesn't really make generics worse. Couple that with the better syntax and less risk for nasty runtime errors, and I think we'll find generics quite useful indeed.

July 21, 2004 В· Jouni Heikniemi В· Comments Closed
Posted in: .NET