c# - c++和c#速度对比

标签 c# c++ benchmarking

当您需要使用原始 CPU 能力时,我担心 C# 在处理繁重计算时的速度。

在计算方面,我一直认为 C++ 比 C# 快得多。所以我做了一些快速测试。第一个测试计算素数 < 整数 n,第二个测试计算一些泛数字。第二次测试的思路来自这里:Pandigital Numbers

C# 素数计算:

using System;
using System.Diagnostics;

class Program
{

    static int primes(int n)
    {

        uint i, j;
        int countprimes = 0;

        for (i = 1; i <= n; i++)
        {
            bool isprime = true;

            for (j = 2; j <= Math.Sqrt(i); j++)

                if ((i % j) == 0)
                {
                    isprime = false;
                    break;
                }

            if (isprime) countprimes++;
        }

        return countprimes;
    }



    static void Main(string[] args)
    {
        int n = int.Parse(Console.ReadLine());
        Stopwatch sw = new Stopwatch();

        sw.Start();
        int res = primes(n);
        sw.Stop();
        Console.WriteLine("I found {0} prime numbers between 0 and {1} in {2} msecs.", res, n, sw.ElapsedMilliseconds);
        Console.ReadKey();
    }
}

C++ 变体:

#include <iostream>
#include <ctime>
#include <cmath>

int primes(unsigned long n) {
unsigned long i, j;
int countprimes = 0;
  for(i = 1; i <= n; i++) {
      int isprime = 1;
      for(j = 2; j < sqrt((float)i); j++) 
          if(!(i%j)) {
        isprime = 0;
        break;
   }
    countprimes+= isprime;
  }
  return countprimes;
}

int main() {
 int n, res;
 cin>>n;
 unsigned int start = clock();

 res = primes(n);
 int tprime = clock() - start;
 cout<<"\nI found "<<res<<" prime numbers between 1 and "<<n<<" in "<<tprime<<" msecs.";
 return 0;
}

当我运行测试试图找到小于 100,000 的素数时,C# 变体在 0.409 秒内完成,C++ 变体在 0.614 秒内完成。 当我运行它们 1,000,000 次时,C# 在 6.039 秒内完成,C++ 在大约 12.987 秒内完成。

C# 中的 Pandigital 测试:

using System;
using System.Diagnostics;

class Program
{
    static bool IsPandigital(int n)
    {
        int digits = 0; int count = 0; int tmp;

        for (; n > 0; n /= 10, ++count)
        {
            if ((tmp = digits) == (digits |= 1 << (n - ((n / 10) * 10) - 1)))
                return false;
        }

        return digits == (1 << count) - 1;
    }

    static void Main()
    {
        int pans = 0;
        Stopwatch sw = new Stopwatch();
        sw.Start();

        for (int i = 1; i <= 123456789; i++)
        {
            if (IsPandigital(i))
            {
                pans++;
            }
        }
        sw.Stop();
        Console.WriteLine("{0}pcs, {1}ms", pans, sw.ElapsedMilliseconds);
        Console.ReadKey();
    }
}

C++ 中的 Pandigital 测试:

#include <iostream>
#include <ctime>

using namespace std;

int IsPandigital(int n)
    {
        int digits = 0; int count = 0; int tmp;

        for (; n > 0; n /= 10, ++count)
        {
            if ((tmp = digits) == (digits |= 1 << (n - ((n / 10) * 10) - 1)))
                return 0;
        }

        return digits == (1 << count) - 1;
    }


int main() {
   int pans = 0;
   unsigned int start = clock();

   for (int i = 1; i <= 123456789; i++)
   {
      if (IsPandigital(i))
      {
        pans++;
      }
   }
   int ptime = clock() - start;
   cout<<"\nPans:"<<pans<<" time:"<<ptime;  
   return 0;
}

C# 变体运行时间为 29.906 秒,C++ 运行时间约为 36.298 秒。

我没有触及任何编译器开关,C# 和 C++ 程序都是使用调试选项编译的。 在我尝试运行测试之前,我担心 C# 会远远落后于 C++,但现在看来 C# 有很大的速度差异。

有人能解释一下吗? C# 是 jitted,而 C++ 是本地编译的,因此 C++ 比 C# 变体更快是正常的。

感谢您的回答!

我已经为发布配置重新进行了所有测试。

第一次测试(素数)

C#(数字 < 100,0000):0.189 秒 C++(数字 < 100,0000):0.036 秒

C#(数字 < 1,000,000):5.300 秒 C++(数字 < 1,000,000):1.166 秒

第二个测试(泛数字):

C#:21.224 秒 C++:4.104 秒

所以,一切都变了,现在 C++ 快多了。我的错误是我运行了调试配置测试。如果我通过 ngen 运行 C# 可执行文件,我能否看到一些速度提升?

我尝试比较 C# 和 C++ 的原因是因为我了解两者的一些基础知识并且我想学习处理 GUI 的 API。我认为 WPF 很好,所以考虑到我的目标是桌面,我想看看 C# 在使用纯粹的 CPU 能力来计算各种计算(文件存档器、密码学、编解码器等)时是否可以提供足够的速度和性能.但遗憾的是,在速度方面,C# 似乎无法跟上 C++ 的步伐。

所以,我假设我将永远被这个问题 Tough question on WPF, Win32, MFC 困住,并且我会更新找到一个合适的 API。

最佳答案

您需要在 Release模式下编译 C++ 并启用优化以获得您正在寻找的性能结果。

关于c# - c++和c#速度对比,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2496803/

相关文章:

C++ O2 在类中使用 const 引用文字时出现内存泄漏

Java:instanceof vs 类名切换性能

C++,缓存局部性改进的基准测试方法?

c# - 持久功能无法启动

c# - Entity Framework 代码优先 - 将两个字段联合到一个集合中

c# - 如何在 C# 中的单个 SQL 查询中更新水果列表的 crate ID

c++ - ActiveQt Com 应用程序示例 - COM 服务器未在 Windows 注册表中注册 (Qt4.7.4)

c# - 第二行代码在第一行之前执行 C#

c++ - 从主函数中删除在另一个函数中使用 new 创建的一系列对象

java - 使用 JMH 作为功能/用户级别性能测试的框架。这是错的吗?