C ++比C#快多少? [英] How much faster is C++ than C#?

查看:173
本文介绍了C ++比C#快多少?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

还是现在是另一回事?

从我听说有一些领域,C#证明比C ++更快,从来没有勇气去测试它自己。

From what I've heard there are some areas in which C# proves to be faster than C++, but I've never had the guts to test it by myself.

以为任何人都可以解释这些细节差异或指向正确的地方了解这方面的信息。 p>

Thought any of you could explain these differences in detail or point me to the right place for information on this.

推荐答案

没有严格的原因为什么一个基于字节码的语言,如C#或Java,有一个JIT不能像C ++代码一样快。然而,C ++代码长期以来明显更快,而且今天在许多情况下仍然如此。这主要是因为更高级的JIT优化实现起来很复杂,而且非常酷的只是刚才到达。

There is no strict reason why a bytecode based language like C# or Java that has a JIT cannot be as fast as C++ code. However C++ code used to be significantly faster for a long time, and also today still is in many cases. This is mainly due to the more advanced JIT optimizations being complicated to implement, and the really cool ones are only arriving just now.

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

So C++ is faster, in many cases. But this is only part of the answer. The cases where C++ is actually faster, are highly optimized programs, where expert programmers thoroughly optimized the hell out of the code. This is not only very time consuming (and thus expensive), but also commonly leads to errors due to over-optimizations.

另一方面,解释语言中的代码在更高版本的运行时(.NET CLR或Java VM)中更快,而不需要执行任何操作。并且有很多有用的优化JIT编译器可以做的是根本不可能的语言与指针。此外,一些人认为垃圾收集通常应该像手动内存管理一样快或快,在许多情况下。

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.

正如Donald Knuth所说的,过早优化是一个非常复杂的过程,所有邪恶的根源。如果你真的知道你的应用程序将主要包括非常关键的性能算术,并且它将是瓶颈,并且它肯定会更快的C + +,你确信C + +不会与其他冲突要求,去C ++。在任何其他情况下,专注于首先以任何最适合您的语言正确实现应用程序,然后如果运行速度太慢,找到性能瓶颈,然后考虑如何优化代码。在最坏的情况下,您可能需要通过外部函数接口调用C代码,因此您仍然能够以低级语言编写关键部分。

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/ 为基准,提出许多怀疑,因为这些基本上是测试算术代码,这很可能与你的代码不一样。

Giving actual percentages of speed advantages is impossible, it largely depends on your code. In many cases, the programming language implementation isn't even the bottleneck. Take the benchmarks at http://benchmarksgame.alioth.debian.org/ with a great deal of scepticism, as these largely test arithmetic code, which is most likely not similar to your code at all.

这篇关于C ++比C#快多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆