Click here to Skip to main content
15,886,786 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to use managed c++ to build resource intensive applications (games mostly) but am not sure if its faster than c# considering the fact that they are both managed. Does anyone know which of the two languages is faster ( I wouldn't mind any statistical data ).

Also wanted to know if managed c++ suffers a lot of performance as compared to normal ANSI c++
Posted

I would expect unmanaged (written well) code is better.... as all the managed stuff runs under the .net framework/runtime.
 
Share this answer
 
Comments
Albert Holguin 9-Jun-11 16:44pm    
(written well) is the keyword...lol... but it also depends on the task...
You might be asking the wrong question.

It is always easier to write good (and faster) code in the language you are most familiar with.

If you are comfortable with C++, then the questions are:

Do you want/need garbage collection?

If you are programming games can you tolerate the non-deterministic nature of garbage collection, or will that impact real-time performance at some point?

What platform(s) are you targeting? Does compiling to the CLR help you with your choice of platforms or hinder you? If you want to target platforms that don't have CLR then native C++ is more portable. If you want to target Window phones, then I think CLR is recommended (required?).

What sort of resources are you talking about? Graphics? Video? Audio? What frameworks / libraries are you going to use to manipulate them? Do those frameworks / libraries offer better support / interfaces for managed or unmanaged code? Unless you are rolling your own library/framework for manipulating those resources, then that's probably going to be the thing that most influences your performance and also dictates your choice.
 
Share this answer
 
I've seen the article which says that C# uses generic optimizer, but C++/CLI uses a special C++/CLI optimizer which can make the code faster then C# in certain situations. Sorry, don't know how to find it… It could be a journal requiring subscription, maybe Dr.Dobbs…

—SA
 
Share this answer
 
v2
Despite the runtime being the same for both C# and C++, there are performance-oriented tricks allowed in C++ language that have no equivalent C# (I mean compile-time programming[^], of course). Using some of these tricks, you could considerably improve efficiency of your program by shifting potentially runtime activity to compile time: the compiler will produce new code while expanding the templates that you provide.

Of course you can produce new code with C# as well, but you would need to emit your own CLR byte code to accomplish this. And if you resort to emitting your own byte code, the language in which you code it becomes very much irrelevant.

I did very lightweight side-by-side comparisons of managed and native 32-bit C++ programs some three years ago (I think it was a CPU-intensive contest-style programming problem, I don't remember the details). I was hoping to get some performance improvement by switching to native 32-bit mode, but I did not get any speed-up worth talking about: wall clock run time in both cases was roughly the same.
 
Share this answer
 
Comments
yesotaso 9-Jun-11 16:36pm    
From C++ -> C#: What You Need to Know to Move "In C++ you have tremendous control over the creation and even the layout of your objects. You can create an object on the stack, on the heap, or even in a particular place in memory using the placement operator new."... Which means you need Totin' Chip Badge to properly use it:) Otherwise it can turn into nuclear hand grenade.
dasblinkenlight 9-Jun-11 16:55pm    
It's worth noting that the article is from 10 years ago, when C# wasn't nearly as powerful as it is today. Even such basic things as generics were missing! As far as the object layout goes, you could write unsafe code to influence your object layout as far back as C# v1.0 (and you didn't need no stinkin' badges to use it back then)
yesotaso 9-Jun-11 17:23pm    
:) Indeed .NET evolved much, but that doesnt change the fact that nuclear hand grenades has a very good blast radius :P
You should also consider what language you and your co-authors likes most, or code is available.

The truth is: Improving the speed is most a task of writing intelligent code, with optimized object creation, file access and most drawing. And you the right drawing API, like OpenGL and not GDI+. Or try both...

my favourite graphic links:
Update Region
Add fast user-extensible image processing support to CBitmap[^]
CxImage[^]
CBitmapEx - Free C++ Bitmap Manipulation Class[^]
ImageStone[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900