本文介绍了为什么C#运行速度比C ++快?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我不是在开玩笑。 我有一个C#应用程序和一个C ++应用程序。他们做同样的事情,在完全相同的代码量...I am not joking.I have a C# application, and a C++ application. They do they exact same thing, in the exact same amount of code... ...和C#一个运行更快,不只是更快,但像10...And the C# one is running faster, not just faster, but like 10 times faster.这让我很奇怪,因为一个,我在调试器中运行C#应用程序,这应该减慢C#开始。然后,由于C#是字节码与巨大的开销使用.NET编译成MSIL和一大堆额外的功能,这应该会放慢速度。This struck me as weird, because for one, I was running the C# app in the debugger, which should slow C# down to begin with. Then, for the reason of that C# is bytecode with huge overhead using .NET compiled into MSIL with a bunch of extra features, that should slow it down. While C++ is just pure machine code.这里是C#代码:static void main(){ ulong i = 0; while (i < 100000000000) { Console.WriteLine(i); i++; }}这是C ++代码int main(){ usigned long i = 0; while (i < 100000000000) { cout << i << endl; i++; } return 0;}他们只是计数和显示一个数字。 C ++一个将在1000,而C#一将在7000.(快7倍)They are just counting and displaying a number. The C++ one would be at 1000, while the C# one would be at 7000. ( 7x faster)我甚至尝试编译他们,并运行他们没有调试器使用命令提示符与命令: cplusplus.exe&& csharp.exeI even tried compiling both of them, and running them without the debugger using command prompt with the command: cplusplus.exe && csharp.exe是的,我知道也许这个问题是offtopic:P或者说不清楚是什么问题。 :/ 但是请有人向我解释这个问题。Yeah, I know maybe this question is "offtopic" :P or maybe it is "not clear what's being asked for". :/But please, someone explain this to me.如果这很重要,我使用这个CPU:Intel i7 2.5 Ghz。If this matters, I am using this CPU: Intel i7 2.5 Ghz.编辑:我做了 cout 创意,以及 std :: ios_base :: sync_with_stdio(false); 在结果中。I did the cout << i << "\n"; idea, plus the std::ios_base::sync_with_stdio(false); idea, without any luck or change in results.编辑2:我试过C的printf(),它是多得多,更快。比C#快3倍。EDIT 2: I tried C's printf() and it was much, much faster. 3x faster than C#.人们告诉我,IO流非常慢,所以我没有写入控制台尝试他们,C ++仍然明显快于C# 。People told me that IO stream was very slow, so I then tried them both without writing to the console, and C++ was still significantly faster than C#.总之,Writeline()比cout快得多,printf()比两者快得多。因此,写入控制台是唯一减慢速度的方法。In conclusion, Writeline() is much faster than cout, and printf() is much faster than both. So writing to the console is the only thing that slows it down. TLDR:printf()胜利,控制台写入速度变慢。TLDR: printf() wins, and console writing slows stuff down.推荐答案我认为你的评价不够仔细。我用C ++重新创建了你的测试,如下所示:I think you've not been careful enough in your evaluation. I recreated your test with C++ proving massively faster as detailed below:using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace CSScratch{ class Program { static void Main(string[] args) { ulong i = 0; while (i < 1000000) { Console.WriteLine(i); i++; } } }} 在VS2013发布模式下构建到CSScratch.exe,然后我计时(在cygwin bash)输出重定向,所以文件系统写入时间不计数。结果相当一致,五次运行的最快 为:I built the above in VS2013 Release mode to CSScratch.exe, which I then timed (under cygwin bash) with output redirected so file-system writing time wasn't counted. Results were fairly consistent, the fastest of five runs being:time ./CSScratch.exe > NULreal 0m17.175suser 0m0.031ssys 0m0.124s C ++等价物:#include <iostream>int main(){ std::ios_base::sync_with_stdio(false); unsigned long i = 0; while (i < 1000000) { std::cout << i << '\n'; i++; }}也用VS2013编译:Also compiled with VS2013:cl /EHsc /O2 output.cctime ./output > NUL 最慢​​的The slowest of five runs:real 0m1.116suser 0m0.000ssys 0m0.109s它比最快的C#运行(17.175秒)更快(1.116秒)。which is still faster (1.116 seconds) than the fastest of C# runs (17.175 seconds).这两个版本的一些时间是通过加载/动态链接,初始化等。我将C ++版本修改为循环10倍以上 它仍然只需要9.327秒 - 大约是C#需要十分之一工作量的时间的一半。Some of the time for both versions is taken by loading / dynamic linking, initialisation etc.. I modified the C++ version to loop 10x more, and it still only took 9.327 seconds - about half the time C# needed for a tenth of the workload.(您可以通过设置更大的输出缓冲区来进一步调整C ++版本,但通常不需要)。(You could further tune the C++ version by setting a larger output buffer, but that's not normally needed). 这篇关于为什么C#运行速度比C ++快?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
07-28 00:43