Thursday, 5 June 2008

Performance of C# vs C++

Many people see C# as a fine language for business applications, but feel it lacks the low-level capabilities of C++ for software which is close to the metal.

C# includes many features such as the unsafe keyword that allows you to achieve most of what you could do in C++, but even without the use of unsafe code performance-intensive programming is possible. I was recently involved in a large project where I had the opportunity to compete with a C++ team, by writing an equivalent program from scratch in C#. Without wishing to break my NDA, the application involved much bit-twiddling, video, networking and the like. Originally I kept in an existing C++ library, but ended up using C# exclusively.

The C# code was around 20% of the size of its C++ equivalent, measured using a sophisticated tool that ignored layout, comments etc. Performance ended up being about the same – there were initial performance problems with the C++ version which were fixed by some serious coding work.

Where I expected the C# version to fall down was with load times, due to the just-in-time compilation. In fact this was very noticeably faster in the C# version – I presume that the JIT overhead was compensated for by the smaller code size.

It would be nice to try something similar with Visual Basic one day!

Labels: , ,