问题描述
我很担心C#的速度时,它重计算,当你需要使用原始CPU处理能力的交易。
I was worried about C#'s speed when it deals with heavy calculations, when you need to use raw CPU power.
我一直认为C ++是比C#快很多当涉及到的计算。所以,我做了一些快速测试。第一个测试计算质数<一个整数n,所述第二测试计算一些pandigital号码。对于第二次测试的想法来自这里:,我很难回答的问题会新找到一个合适的API
So, I'm assuming I will be forever stuck with this question Tough question on WPF, Win32, MFC, and I'll newer find an suitable API.
推荐答案
为什么你会认为即时编译的代码比本地代码慢?唯一的速度损失将是实际jitting,这只会发生一次(一般来说)。给定一个计划用30秒的运行时间,我们正在谈论的总成本中微不足道的一部分。
Why would you assume that jitted code is slower than native code? The only speed penalty would be the actual jitting, which only happens once (generally speaking). Given a program with a 30-second running time, we are talking about a minuscule portion of the total cost.
我想你可能是混乱的即时编译代码的解释的代码,这被编译线逐线。 。有两间漂亮的显著差异
I think you may be confusing jitted code with interpreted code, which is compiled line-by-line. There's a pretty significant difference between the two.
正如其他人所指出的那样,你还需要在释放模式运行这个;调试模式转弯的关闭的最优化,所以这两个版本将慢于他们应该(但不同的量)。
As others have pointed out, you also need to run this in release mode; debug mode turns off most optimizations, so both versions will be slower than they should be (but by different amounts).
修改 - 我要指出的另一件事情,这是该行:
Edit - I should point out one other thing, which is that this line:
for (j = 2; j <= Math.Sqrt(i); j++)
是令人难以置信的效率低下,可能会与基准干扰。你应该计算的Math.sqrt(我)
的之外的内部循环。这是可能的,这将通过等量两个版本放缓,但我不知道,不同的编译器将执行不同的优化。
Is incredibly inefficient and may interfere with the benchmarking. You should be calculating Math.Sqrt(i)
outside of the inner loop. It's possible that this will slow down both versions by an equivalent amount, but I'm not sure, different compilers will perform different optimizations.
这篇关于C ++和C#速度相比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!