还是现在反过来了?

据我所知,c#在某些领域被证明比c++更快,但我从来没有勇气亲自测试它。

我想你们任何人都可以详细解释这些差异,或者告诉我有关信息的正确位置。


当前回答

我在c++ vs c#中插入代码的一个领域是创建到SQL Server的数据库连接并返回结果集。我比较了c++ (ODBC上的薄层)和c# (ADO。NET SqlClient),并发现c++比c#代码快50%左右。ADO。NET被认为是处理数据库的低级接口。你可能会发现更大的差异是内存消耗而不是原始速度。

使c++代码更快的另一件事是,你可以在粒度级别上调优编译器选项,以一种在c#中无法做到的方式进行优化。

其他回答

没有严格的理由说明为什么基于字节码的语言(如c#或Java)不能像c++代码一样快。然而,c++代码在很长一段时间内都要快得多,今天在许多情况下仍然如此。这主要是因为更高级的JIT优化实现起来比较复杂,而真正酷的JIT优化现在才出现。

所以在很多情况下,c++更快。但这只是答案的一部分。c++实际上更快的情况是高度优化的程序,其中专业程序员彻底优化了代码。这不仅非常耗时(因此非常昂贵),而且由于过度优化通常会导致错误。

On the other hand, code in interpreted languages gets faster in later versions of the runtime (.NET CLR or Java VM), without you doing anything. And there are a lot of useful optimizations JIT compilers can do that are simply impossible in languages with pointers. Also, some argue that garbage collection should generally be as fast or faster as manual memory management, and in many cases it is. You can generally implement and achieve all of this in C++ or C, but it's going to be much more complicated and error prone.

As Donald Knuth said, "premature optimization is the root of all evil". If you really know for sure that your application will mostly consist of very performance critical arithmetic, and that it will be the bottleneck, and it's certainly going to be faster in C++, and you're sure that C++ won't conflict with your other requirements, go for C++. In any other case, concentrate on first implementing your application correctly in whatever language suits you best, then find performance bottlenecks if it runs too slow, and then think about how to optimize the code. In the worst case, you might need to call out to C code through a foreign function interface, so you'll still have the ability to write critical parts in lower level language.

请记住,优化一个正确的程序相对容易,但更正一个优化的程序要难得多。

给出实际的速度优势百分比是不可能的,这在很大程度上取决于你的代码。在许多情况下,编程语言实现甚至不是瓶颈。请带着极大的怀疑态度使用http://benchmarksgame.alioth.debian.org/上的基准测试,因为这些测试的主要是算术代码,很可能与您的代码完全不同。

对于图形来说,标准的c#图形类比通过C/ c++访问的GDI慢得多。 我知道这与语言本身无关,更多的是与整个。net平台有关,但是图形是作为GDI替代品提供给开发人员的,它的性能非常糟糕,我甚至不敢用它来处理图形。

我们有一个简单的基准来查看图形库的速度,那就是在窗口中随机绘制线条。c++ /GDI在处理10000行代码时仍然很灵活,而c# /Graphics在处理1000行代码时却很难做到。

c++(或C)为您提供了对数据结构的细粒度控制。如果你想玩,你有这个选择。大型托管Java或。net应用程序(OWB, Visual Studio 2005),它们使用Java/。NET库自带包袱。我曾见过OWB设计会话使用超过400 MB的RAM,立方体或ETL设计的bid也达到100 MB。

在可预测的工作负载上(例如重复一个过程多次的大多数基准测试),JIT可以为您提供优化得足够好的代码,没有实际差别。

在大型应用程序上,差异与其说是JIT,不如说是代码本身使用的数据结构。当应用程序的内存很重时,您将获得较低的缓存使用效率。在现代cpu上,缓存丢失是非常昂贵的。C或c++的真正优势在于,您可以优化数据结构的使用,从而更好地使用CPU缓存。

. net语言可以像c++代码一样快,甚至更快,但是c++代码将拥有更恒定的吞吐量,因为. net运行时必须暂停进行GC,即使它非常巧妙地处理了暂停。

因此,如果您有一些代码必须持续快速运行而不需要任何暂停,. net在某些时候会引入延迟,即使您非常小心地使用运行时GC。

We have had to determine if C# was comparable to C++ in performance and I wrote some test programs for that (using Visual Studio 2005 for both languages). It turned out that without garbage collection and only considering the language (not the framework) C# has basically the same performance as C++. Memory allocation is way faster in C# than in C++ and C# has a slight edge in determinism when data sizes are increased beyond cache line boundaries. However, all of this had eventually to be paid for and there is a huge cost in the form of non-deterministic performance hits for C# due to garbage collection.