Benefits with C++ in modern apps

Why would one want to use C++ when developing modern applications for Windows? A lot of developers see C++ as an old, aging, clumsy giant. Why program using C++ when you can be much more productive using a modern high level language like C#? Usually a developer doesn’t have any reason to use C++. A lot of the applications we are writing today are applications that consume data; get some data from a service, make some trivial calculations and display it. Usually nothing that is CPU intensive.

Arithmetics and vectorization

But then there are the benefits. C++ can in some situations bring with it big performance improvements. Especially when it comes to vectorizable arithmetics. Because then the C++ compiler can take advantage of the different kind of SSE instructions available in modern CPUs and make those operations really fast. Such operations can be common when writing photo editing applications.

Memory management

Memory management is another big possible difference. C++ can be written to execute without a garbage collector. A garbage collector [GC] will basically run from time to time and remove all unused objects in your code. This comes with the disadvantage that the GC will use up resources when it runs and that it might run at inappropriate times and some times even not run when you want it to. This ultimately means you have no fine control of the amount of memory allocated.

It is important to keep in mind that managing memory manually does not automatically imply faster running code. Ultimately it is up to you and your skill as a programmer to write fast code. Simply switching your code to run C++ won’t necessarily improve performance.

Lab example

A while ago we had a competence day at the office where we benchmarked some very basic operations using C++ and C# to determine which was faster. We wrote some very simple operations in C# as you can see here:

var firstValue = 1.0;
var result = 1.0;

var timeBefore = DateTime.Now;

for (var i = 0; i < 100000000; i++)
{
    var oldResult = result;
    result = firstValue + result;
    firstValue = oldResult;
}

var timeAfter = DateTime.Now;
Time = (timeAfter - timeBefore).TotalMilliseconds;

 

And the same operations using C++ code as you can see here:

double firstValue = 1.0;
double result = 1.0;

SYSTEMTIME before;
SYSTEMTIME after;

GetSystemTime(&before);

for (int i = 0; i < 100000000; i++)
{
    int oldResult = result;
    result = firstValue + result;
    firstValue = oldResult;
}

GetSystemTime(&after);

double i = GetMilliseconds(after) - GetMilliseconds(before);
Time->Text = i.ToString();

 

To make sure we have a fair test, both builds were set to build a release build for x64. I ran each build ten times. Consistently the C++ executable finishes in 175ms with a variation of ±1ms. The C# executable runs almost twice as slow and finishes at 307ms ±10ms.

Mix and match

One thing to keep in mind is that you don’t have to choose one and write your entire application in it. It is possible to write most of your code using C# and then just have a project in C++ that you can delegate compute intensive tasks to.

Further reading

Building a UI: What does it cost?

Petar Mataic

Petar is an ambitious young developer who hungers for new technology. Because new technology often means that his work becomes easier and allow him to do things faster, better, cooler. Experienced in .NET, he is currently focused on the iOS platform. Born in a country that doesn't exist anymore, he aspires to make the whole world his home, sharing his passion with other software developers attending and speaking at international conferences.

This Post Has 5 Comments

  1. why is this so that C++ executes faster than C# ?

    1. This is actually a very complex topic which I unfortunately couldn’t explain very well in a short comment. I would suggest you check out this question and the discussion on StackOverflow: http://stackoverflow.com/questions/418914/why-is-c-so-fast-and-why-arent-other-languages-as-fast-or-faster. It mainly concerns C but could be applied to C++ in some extent as well.
      There’s also this question which concerns C++ vs C# directly: http://stackoverflow.com/questions/138361/how-much-faster-is-c-than-c

    2. Because C# uses an interpreter and virtual machine to execute code and C++ compiles to native code which can be executed directly by the operating system.

  2. Highly descriptive post, I liked that a lot.

Leave a Reply

Close Menu