I thought I’d comment on my own post about C# vs C++ performance. The purpose of the test was to compare the time to sort 128-byte objects in C++ vs C#, nothing more. Again if you understood that, then you can propbably see that the performance measured has more to do with execution envrionments than languange differences.

That said, I’d like to address out the following issues in my testing:

I used a char array in C# to simulate a similar char array in C++. This incorrect since C# chars are 16 bits while C++ they are only 8 bits. I admit this was pretty sloppy. Replacing char with byte fixes this, and the result is the C# version runs about 15% faster. Faster, yes, but still not fast enough to make a significant difference. The most striking thing is that halving the memory usage only gives a 15% difference, in C++ the memory had direct linear impact on runtime. Some people complained about using the fixed keyword to make the array part of the Data struct. It’s trivial to replace it with a number of int or float to create a 128-byte struct and makes no difference in performance. The point was to approximate C++ and to represent a larger class, fixed was a simple way to do this. Some people complained this isn’t an apple to apple comparison – I disagree. It’s about as direct a way as possible of comparing two very different languages and operating environments – perform the same task and see how long it takes. I was running Visual Studio 2005 SP1 for both tests, newer/different versions may have varying results.

The purpose of the previous post isn’t to claim that C++ is better than C#, but many people seem to think that I am – I suppose this is what I get for posting such an inflammatory title. I use C# on a daily basis and like it.

There were a number of comments on my last post claiming to “debunk” my tests referring to what is posted here. This debunking itself admits it’s comparing apples to oranges – however it completely misses the point of creating a fixed size array in the first place. Replacing the fixed array with a heap pointer shows nothing about C# performance – only that copying a smaller struct is faster than copying a larger one. This is a fact I addressed previously.

The claim is made that this test isn’t representative of what a C# programmer would write. True, a C# programmer would very rarely used a struct with a fixed array. However, it’s fairly easy to create objects with 128-bytes or more of data. That’s 32 4-bytes variables, which isn’t a huge amount of variables to have in a class, especially one with a few parent classes each with a few member variables.

Anyways, other that the char/byte mistake, I stand by the original results – C# takes about 10 times longer to sort an array on my computer, compared to a similar C++ verision.