还是现在反过来了?

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

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


当前回答

这实际上取决于你想在代码中实现什么。我听说这只是一个都市传说,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++在有大型数组或数组(任何大小)上的大量循环/迭代的程序中可以表现得更好。这就是为什么在C/ c++中图形化通常要快得多,因为几乎所有的图形化操作都基于繁重的数组操作。net在数组索引操作中是出了名的慢,这是由于所有的安全检查,这对于多维数组尤其如此(是的,矩形c#数组甚至比锯齿形c#数组还要慢)。

The bonuses of C/C++ are most pronounced if you stick directly with pointers and avoid Boost, std::vector and other high-level containers, as well as inline every small function possible. Use old-school arrays whenever possible. Yes, you will need more lines of code to accomplish the same thing you did in Java or C# as you avoid high-level containers. If you need a dynamically sized array, you will just need to remember to pair your new T[] with a corresponding delete[] statement (or use std::unique_ptr)—the price for the extra speed is that you must code more carefully. But in exchange, you get to rid yourself of the overhead of managed memory / garbage collector, which can easily be 20% or more of the execution time of heavily object-oriented programs in both Java and .NET, as well as those massive managed memory array indexing costs. C++ apps can also benefit from some nifty compiler switches in certain specific cases.

I am an expert programmer in C, C++, Java, and C#. I recently had the rare occasion to implement the exact same algorithmic program in the latter 3 languages. The program had a lot of math and multi-dimensional array operations. I heavily optimized this in all 3 languages. The results were typical of what I normally see in less rigorous comparisons: Java was about 1.3x faster than C# (most JVMs are more optimized than the CLR), and the C++ raw pointer version came in about 2.1x faster than C#. Note that the C# program only used safe code—it is my opinion that you might as well code it in C++ before using the unsafe keyword.

Lest anyone think I have something against C#, I will close by saying that C# is probably my favorite language. It is the most logical, intuitive and rapid development language I've encountered so far. I do all my prototyping in C#. The C# language has many small, subtle advantages over Java (yes, I know Microsoft had the chance to fix many of Java's shortcomings by entering the game late and arguably copying Java). Toast to Java's Calendar class anyone? If Microsoft ever spends real effort to optimize the CLR and the .NET JITter, C# could seriously take over. I'm honestly surprised they haven't already—they did so many things right in the C# language, why not follow it up with heavy-hitting compiler optimizations? Maybe if we all beg.

我已经在c++和c#等效中测试了vector - List和简单的2d数组。

我使用Visual c# / c++ 2010 Express版本。这两个项目都是简单的控制台应用程序,我在标准(没有自定义设置)发布和调试模式下对它们进行了测试。 c#列表在我的电脑上运行得更快,c#中的数组初始化也更快,数学运算更慢。

我使用英特尔Core2Duo P8600@2.4GHz, c# - . net 4.0。

我知道向量实现不同于c#列表,但我只是想测试我将用于存储我的对象的集合(并能够使用索引访问器)。

当然,您需要清除内存(比如每次使用new时),但我希望保持代码简单。

c++矢量测试:

static void TestVector()
{
    clock_t start,finish;
    start=clock();
    vector<vector<double>> myList=vector<vector<double>>();
    int i=0;
    for( i=0; i<500; i++)
    {
        myList.push_back(vector<double>());
        for(int j=0;j<50000;j++)
            myList[i].push_back(j+i);
    }
    finish=clock();
    cout<<(finish-start)<<endl;
    cout<<(double(finish - start)/CLOCKS_PER_SEC);
}

c#列表测试:

private static void TestVector()
{

    DateTime t1 = System.DateTime.Now;
    List<List<double>> myList = new List<List<double>>();
    int i = 0;
    for (i = 0; i < 500; i++)
    {
        myList.Add(new List<double>());
        for (int j = 0; j < 50000; j++)
            myList[i].Add(j *i);
    }
    DateTime t2 = System.DateTime.Now;
    Console.WriteLine(t2 - t1);
}

c++ -数组:

static void TestArray()
{
    cout << "Normal array test:" << endl;
    const int rows = 5000;
    const int columns = 9000;
    clock_t start, finish;

    start = clock();
    double** arr = new double*[rows];
    for (int i = 0; i < rows; i++)
        arr[i] = new double[columns];
    finish = clock();

    cout << (finish - start) << endl;

    start = clock();
    for (int i = 0; i < rows; i++)
        for (int j = 0; j < columns; j++)
            arr[i][j] = i * j;
    finish = clock();

    cout << (finish - start) << endl;
}

c# -数组:

private static void TestArray()
{
    const int rows = 5000;
    const int columns = 9000;
    DateTime t1 = System.DateTime.Now;
    double[][] arr = new double[rows][];
    for (int i = 0; i < rows; i++)
        arr[i] = new double[columns];
    DateTime t2 = System.DateTime.Now;

    Console.WriteLine(t2 - t1);

    t1 = System.DateTime.Now;
    for (int i = 0; i < rows; i++)
        for (int j = 0; j < columns; j++)
            arr[i][j] = i * j;
    t2 = System.DateTime.Now;

    Console.WriteLine(t2 - t1);

}

时间:(发布/调试)

C++

600 / 606 ms array init 200 / 270毫秒阵列填充, 1秒/13秒矢量初始化和填充。

(是的,13秒,我总是在调试模式下遇到列表/向量的问题。)

C#:

20 / 20 ms数组初始化 403 / 440毫秒阵列填充, 710 / 742 ms列表初始化和填充。

这是一个非常模糊的问题,没有真正明确的答案。

例如;我宁愿玩用c++而不是c#创建的3d游戏,因为性能肯定要好得多。(我知道XNA等,但它与真正的东西相去甚远)。

另一方面,如前所述;您应该使用一种能够让您快速完成所需工作的语言进行开发,并在必要时进行优化。

需要大量内存访问的应用程序。图像处理通常更适合在非托管环境(c++)而不是托管环境(c#)中编写。使用指针算法优化的内循环在c++中更容易控制。在c#中,你可能需要使用不安全的代码来获得相同的性能。

我发现2020年4月读过:https://www.quora.com/Why-is-C-so-slow-compared-to-Python,作者是一位拥有15年以上软件开发经验的现实世界的程序员。

它指出,c#通常较慢,因为它被编译为公共中间语言(CIL),而不是像c++那样的机器代码。然后,CIL通过公共语言运行库(CLR)输出机器代码。但是,如果您继续执行c#,它将获取机器代码的输出并缓存它,以便为下次执行保存机器代码。总而言之,如果多次执行,c#会更快,因为它是多次执行后的机器代码。

也有评论说,一个优秀的c++程序员可以做一些耗时的优化,但最终会被优化。