还是现在反过来了?

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

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


当前回答

快了5个橘子。或者更确切地说:不可能有一个(正确的)笼统的答案。c++是一种静态编译语言(但也有配置文件引导的优化),c#在JIT编译器的帮助下运行。它们之间的差异如此之大,以至于像“快了多少”这样的问题都无法回答,甚至无法给出数量级。

其他回答

这实际上取决于你想在代码中实现什么。我听说这只是一个都市传说,VB和VB之间有性能上的差异。NET, c#和托管c++。然而,我发现,至少在字符串比较中,托管c++胜过c#,而c#又胜过VB.NET。

我并没有对这两种语言的算法复杂度做过详尽的比较。我也只是使用每种语言的默认设置。在VB。NET我使用设置要求声明变量,等等。下面是我用于托管c++的代码:(正如你所看到的,这段代码非常简单)。我用。net 4.6.2在Visual Studio 2013的其他语言中运行相同的程序。

#include "stdafx.h"

using namespace System;
using namespace System::Diagnostics;

bool EqualMe(String^ first, String^ second)
{
    return first->Equals(second);
}
int main(array<String ^> ^args)
{
    Stopwatch^ sw = gcnew Stopwatch();
    sw->Start();
    for (int i = 0; i < 100000; i++)
    {
        EqualMe(L"one", L"two");
    }
    sw->Stop();
    Console::WriteLine(sw->ElapsedTicks);
    return 0;
}

在一个特殊的场景中,c++仍然占据上风(并且将在未来几年占据上风),即可以在编译时预先确定多态决策。

通常,封装和延迟决策是一件好事,因为它使代码更加动态,更容易适应不断变化的需求,并且更容易作为框架使用。这就是为什么在c#中面向对象编程是非常高效的,并且它可以在术语“泛化”下泛化。不幸的是,这种特殊的泛化在运行时是有代价的。

Usually, this cost is non-substantial but there are applications where the overhead of virtual method calls and object creation can make a difference (especially since virtual methods prevent other optimizations such as method call inlining). This is where C++ has a huge advantage because you can use templates to achieve a different kind of generalization which has no impact on runtime but isn't necessarily any less polymorphic than OOP. In fact, all of the mechanisms that constitute OOP can be modelled using only template techniques and compile-time resolution.

在这种情况下(不可否认,它们通常局限于特殊的问题领域),c++胜过c#和类似的语言。

通常,这取决于应用程序。在某些情况下,c#可能慢得可以忽略不计,而在其他情况下,c++要快5到10倍,特别是在操作可以轻松SIMD的情况下。

I suppose there are applications written in C# running fast, as well as there are more C++ written apps running fast (well C++ just older... and take UNIX too...) - the question indeed is - what is that thing, users and developers are complaining about ... Well, IMHO, in case of C# we have very comfort UI, very nice hierarchy of libraries, and whole interface system of CLI. In case of C++ we have templates, ATL, COM, MFC and whole shebang of alreadyc written and running code like OpenGL, DirectX and so on... Developers complains of indeterminably risen GC calls in case of C# (means program runs fast, and in one second - bang! it's stuck). To write code in C# very simple and fast (not to forget that also increase chance of errors. In case of C++, developers complains of memory leaks, - means crushes, calls between DLLs, as well as of "DLL hell" - problem with support and replacement libraries by newer ones... I think more skill you'll have in the programming language, the more quality (and speed) will characterize your software.

没有严格的理由说明为什么基于字节码的语言(如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/上的基准测试,因为这些测试的主要是算术代码,很可能与您的代码完全不同。