开发者

C# vs C++ performance comparison [duplicate]

This question already has answers here: Closed 12 years ago.

Possible Duplicate:

开发者_运维技巧 How much faster is C++ than C#?

Hello!

Are there any cases in which C# is faster (has better performance) than C++ in practical use?

I heard that generic collections are a significant performance advantage over stl - is that true?

Has native code written in C# (unsafe block, pin pointers, Marshal...) the same performance as the same code written natively in C++?


Are there any cases in which C# is faster (has better performance) than C++ in practical use?

and

Has native code written in C# (unsafe block, pin pointers, Marshal...) the same performance as the same code written natively in C++?

Yes, there are times when this can happen. See the answer here:

Why would I see ~20% speed increase using native code?

I heard that generic collections are a significant performance advantage over stl - is that true?

Not necessarily. STL can be incredibly efficient - often more so than generic collections in .NET.

However, in general, I wouldn't focus on performance at this level. C# and C++ are both "fast enough" if you develop in them correctly. You can make very, very performant code in either language - and just as easily, you can make code that performs horribly in either language.


I heard that generic collections are a significant performance advantage over stl - is that true?

I highly doubt it, the STL uses templates, which gets around JIT overhead and still creates true, compiled, statically typed collections.

Has native code written in C# (unsafe block, pin pointers, Marshal...) the same performance as the same code written natively in C++?

Although C# unsafe code performs very well, it doesn't tie into the rest of the runtime very well... For example, try to do socket code with unsafe buffers, and you just end up using fixed blocks everywhere, and it turns into a nightmare. Not only that, C++ code will still perform better.

Are there any cases in which C# is faster (has better performance) than C++ in practical use?

Dynamic code is the biggest one that comes to mind, System.Reflection.Emit and Linq expressions (especially with the new features in C# 4.0) really make code generation in C# practical, whereas similar strategies in C++ might take significantly more effort (and therefore not be practical).


I have had some performance improvement using C# instead of C++ for an application which was allocating/deallocating a lot of small sized objects, but with different sizes. I suspect garbage collection is a good tool for such jobs.

However, at least with C#/.NET 2.0, C++ yields definitely faster code for crunching number arrays (there is no SSE support for c# 2.0).


As far as I have understood .NET you are not able to write native code in C#. Even unsafe code is managed, it just has more access to system ressources and thus more responsiblity to clean up afterwards.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜