Share via


Optimizing managed C# vs. native C++ code

Raymond Chen (aka "fixed more Windows bugs than you've had hot dinners") and Rico Mariani (aka "Mr .NET Performance") have been running a great series of articles where they write and then optimize the same application in two different languages: native C++ and managed C#. The easiest starting point for the two sets of articles is "Performance Quiz #6 -- Chinese/English Dictionary reader".

There's a lot of good information in both the articles and the reader comments, and if you've got the time they're well worth studying for hints on how to write and benchmark high-performance code. The summary is a compelling argument for .NET:

  • A line-for-line translation of the original C++ code into C# ran 10 times faster than the C++ code.
  • It took five different optimizations (one of which introduced a bug) for the C++ code to match the speed of the unaltered C# code.
  • After Raymond's sixth optimization, his C++ code finally beat the C# code — because the runtime got down to where the 60ms startup overhead of the CLR made a difference!
  • To accomplish this, Raymond had to:
    • Write his own file/io stuff
    • Write his own string class
    • Write his own allocator
    • Write his own international mapping

So yes, C++ code can be faster than C# code — but when you look at all the work Raymond had to go through to achieve that, you have to ask yourself "is it worth it?"

Edit: Jeff Atwood has posted a graph illustrating the diminishing returns of Raymond's optimization efforts.

Comments

  • Anonymous
    May 20, 2005
    I must say the way raymond uses C++ strikes me as odd.. more like C with some stuff.

    A struct of wstrings in a vector was too slow; so instead he jumped to storing wchar arrays I believe; why? He could've just made the struct a pointer in the first place. Or the wstrings pointers for that matter.

    He also didnt reserve a decent sized vector up front, nor did he think to use a boost memory pool for the wstrings or similar approaches; He choose to go lowlevel, even though theres vastly superior options available.
  • Anonymous
    May 20, 2005
    Say you want to e-mail such a program to a person who you know will run it on a plain Win98 system.

    What size of stuff do you have to send to run the C# version, and what size of stuff do you have to send to run the C++ version?

    That's why the C++ version is "worth it" in a lot of circumstances.
  • Anonymous
    May 21, 2005
    How much is it worth to build applications for systems that are nearly 8 years old?
  • Anonymous
    May 21, 2005
    @Michael: Win98? Tell me your not developing new stuff for Win98? What good does that do the world?
  • Anonymous
    May 23, 2005
    Win98 was just one example - if you don't like Win98, then how about if the person is running a fully up-to-date Windows XP SP2 installation that was updated to SP2 over the Internet?

    They might not have the .NET runtime on it, so if you want to send them this little program you wrote, you also have to e-mail them the entire .NET runtime as well.
  • Anonymous
    May 25, 2005
    The comment has been removed