C# code is not inherently slow, and writing code in C++ does not guarantee that your programs will execute fast. For me, these facts were again highlighted by taking part in last month’s Programming Challenge from the C/C++/C# Blog by David Bolton at About.com (which is part of The New York Times Company). The challenge was to calculate a 20x20 Knight’s Tour in the fastest possible time. The final results are out, and my C++ and C# entries ended up in the first and third places, respectively. When comparing my two entries with each other, one can get an idea of how C# and C++ perform when they implement essentially the same logic. Furthermore, one can compare the performance of this C# entry with all the C++ entries and realize that C# code can in fact be very fast.
The actual performance of C# code depends somewhat on the hardware that the C# code is executed on and JIT-compiled, as well as the choices between Mono or the Microsoft .NET Framework and the version of whatever framework is used. Together, these factors can easily double or halve your execution time. But among all the C++ entries, the average time was about 1 millisecond to calculate a tour, which is quite fast for the challenge involving the specified board size; it can easily take thousands or millions times longer for brute-force algorithms that include backtracking. Still, that makes my C# entry more than 20 times faster than the average C++ entry. The point is, even C++ code can be relatively slow, often much slower than C# code. When it comes to writing fast code, it is more about the algorithm and the way you structure your code than it is about your choice of programming language.
Having said that, C++ code can still beat C# code in terms of speed, as illustrated by the drastically different performance of my C# versus C++ entries; the core fragments that were measured for the challenge are almost identical between the two entries. Despite the similarities, the C# version ends up roughly two (or perhaps three) times slower when compared to the C++ version; generally I would expect a difference in the order of only 15% to 30%. So, the difference in this specific case was much bigger than I expected, but this is probably something that cannot be generalized. Nonetheless, my C# entry still ended up being very fast in comparison with the average C++ entry. So, when performance is absolutely essential, prefer C++ over C#, but then first make sure you get the algorithm and structure of the code right or it won’t matter anyway.
The code that I wrote and submitted for Programming Challenge 35 can be downloaded from the links at the end. The software is released under the MIT License (those files that include the license header). The programs implement the Knight’s Tour using Warnsdorff’s Algorithm. It is also interesting to note that, although the C# program uses unsafe code to eliminate the bounds checking for arrays, the resulting performance gain is only in the order of about 10%, and, if you are looking for a “safe” implementation, you would need to change only two or three lines of code.
Overall, I think C# is a good language in terms of general execution speed and a developer’s productivity gains resulting from the .NET Framework; for example, my C# entry also includes code to render the Knights Tour as an image (similar to the one shown below), which would be much more difficult to implement if you insisted on using C++ alone. Of course, you can always combine C++ and the power of .NET through C++/CLI, if you want.