还是现在反过来了?

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

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


当前回答

我想这么说:编写更快代码的程序员,是那些更了解当前机器运行速度的人,顺便说一句,他们也是那些使用适当工具的人,这些工具允许精确的低级和确定性优化技术。由于这些原因,这些人使用C/ c++而不是c#。我甚至认为这是事实。

其他回答

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

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

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

In theory, for long running server-type application, a JIT-compiled language can become much faster than a natively compiled counterpart. Since the JIT compiled language is generally first compiled to a fairly low-level intermediate language, you can do a lot of the high-level optimizations right at compile time anyway. The big advantage comes in that the JIT can continue to recompile sections of code on the fly as it gets more and more data on how the application is being used. It can arrange the most common code-paths to allow branch prediction to succeed as often as possible. It can re-arrange separate code blocks that are often called together to keep them both in the cache. It can spend more effort optimizing inner loops.

我怀疑。net或任何jre都能做到这一点,但早在我上大学的时候就有人在研究这一点,所以认为这类东西很快就会在现实世界中找到自己的方式也不是不合理的。

受此启发,我做了一个快速测试,使用了大多数程序所需的60%的通用指令。

下面是c#代码:

for (int i=0; i<1000; i++)
{
    StreamReader str = new StreamReader("file.csv");
    StreamWriter stw = new StreamWriter("examp.csv");
    string strL = "";
    while((strL = str.ReadLine()) != null)
    {
        ArrayList al = new ArrayList();
        string[] strline = strL.Split(',');
        al.AddRange(strline);
        foreach(string str1 in strline)
        {
            stw.Write(str1 + ",");
        }
        stw.Write("\n");
    }
    str.Close();
    stw.Close();
}

字符串数组和数组列表是特意用来包含这些指令的。

下面是c++代码:

for (int i = 0; i<1000; i++)
{
    std::fstream file("file.csv", ios::in);
    if (!file.is_open())
    {
        std::cout << "File not found!\n";
        return 1;
    }

    ofstream myfile;
    myfile.open ("example.txt");
    std::string csvLine;

    while (std::getline(file, csvLine))
    {
        std::istringstream csvStream(csvLine);
        std::vector csvColumn;
        std::string csvElement;

        while( std::getline(csvStream, csvElement, ‘,’) )
        {
            csvColumn.push_back(csvElement);
        }

        for (std::vector::iterator j = csvColumn.begin(); j != csvColumn.end(); ++j)
        {
            myfile << *j << ", ";
        }

        csvColumn.clear();
        csvElement.clear();
        csvLine.clear();
        myfile << "\n";
    }
    myfile.close();
    file.close();
}

我使用的输入文件大小为40 KB。

结果是

c++代码在9秒内运行。 c#代码:4秒!!

哦,但是这是在Linux上…在Mono上运行c#…c++和g++。

好的,这是我在Windows - Visual Studio 2003上得到的:

c#代码在9秒内运行。 c++代码——可怕的370秒!!

没有严格的理由说明为什么基于字节码的语言(如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#的主要问题是内存消耗高,而且我还没有找到控制它的好方法。最终导致。net软件变慢的是内存消耗。

另一个因素是JIT编译器不能提供太多时间来进行高级优化,因为它在运行时运行,如果花费太多时间,最终用户会注意到它。另一方面,c++编译器有足够的时间在编译时进行优化。恕我直言,这个因素远没有内存消耗那么重要。