无法解释的速度测试C#与C ++? [英] Unexplainable speed test C# vs C++?

查看:93
本文介绍了无法解释的速度测试C#与C ++?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了检查C#和C ++之间的速度差异,我制作了两个非常基本的程序.结果并没有什么不同,但是令人惊讶的事情正在发生,我无法解释. Bot程序具有执行以下操作的基本for循环:

I have made two very basic programs for the purpose of checking the difference in speed between C# and C++. The results where not that different, but something amazing is happening that I cant explain. Bot programs have a basic for loop that does the following:

int i;
for (int n=1;n<999999999;n++)
     i = (n * n) / n; //Small calculation to use some processing power



如果我运行C ++或C#程序,它们平均要花14到16秒,而C#程序通常要快0.5秒左右.但是,当我尝试通过同时运行它们来测试程序时,它们的完成速度快50%.这不是随机发生的.此后我做了几次,而且速度始终更快.

我从来没有在大学学习过,只从阅读书籍和在线材料中学到了东西,所以请原谅我这是不是真的很愚蠢,但是怎么会发生呢?



If I run the C++ or C# program, they average between 14 - 16 seconds, with the C# program usually around .5 seconds quicker. However, when I tried to test the programs by running them at the same time, they finished 50% quicker. This was not a random occurrence. I did this a few times afterwards and it is consistently much faster.

I have never studied at a University level and have only learned from reading books and online material so forgive me if this is a really stupid little thing, but how can this be happening?

推荐答案

如果两个程序都需要n秒才能运行,这并不意味着您将需要2 * n秒才能同时运行两个程序.

如果您的处理器支持超线程,或者是双核(甚至是四核),则这两个程序将同时"运行,并且基本上还需要n秒才能完成.

是你的意思吗?

----------------------------

您的系统上一定有某些东西会使您的程序有时等待.我建议您正确地测量时间:

而不是减去程序的结束和开始时间,在C#中使用Process.TotalProcessorTime在常规C(WIN32)中使用GetProcessTimes.我相信结果会是一样的,绝对没有理由为什么同一程序的运行速度会比您描述的快2倍.
If both programs need n seconds to run, it doesn''t mean you will need 2*n seconds to run both of them at the same time.

If your processor supports hyper-threading, or is a dual-core (or even quad-core), then both programs will run "at the same time" and will need basically still n seconds to finish.

Is it what you mean?

----------------------------

There must be something on your system that make your program wait sometimes. I suggest that you measure the time properly:

Instead of subtracting the ending and starting time of your program, use Process.TotalProcessorTime in C# and GetProcessTimes in regular C (WIN32). I am sure the results will be quite the same, there is absolutely no reason why the same program would run 2 times faster as you describe.


我的猜测是操作系统注意到它是在一定数量的CPU负载下停止运行,并停止运行其某些后台任务,从而为完成分配给它的任务分配更多的功能.不过这很奇怪.

顺便说一下,您的算术运算将导致i> sqrt(MAX_INT),尽管如果您不使用结果,我想您也不在乎.
My guess is that the OS notices that it is under a certain amount of CPU load and stops running some of its background tasks, allocating more power to completing the tasks you''ve given it. It is quite odd though.

By the way your arithmetic will result in an overflow and nonsense answers for i > sqrt(MAX_INT), though if you''re not using the result I guess you don''t care.


如果您以发布模式(完全优化的代码)编译C ++应用程序,并且如果不进行管理,它将更快,因为编译器将意识到该循环是无用的(从不使用计算值i)并将其删除.

尽管我尚未计时非托管C ++的调试版本,但它似乎与C#代码大致相似(在使用了几年时间的笔记本电脑上,在8到9秒之间).
If you compile you C++ application in Release mode (fully optimized code) and not managed, it will be much faster as the compiler will realize that the loop is useless (computed value i is never used) and will removed it.

Although I haven''t timed the debug version of unmanaged C++, it appears to be roughly similar to C# code (between 8 and 9 seconds on a few years old laptop).


这篇关于无法解释的速度测试C#与C ++?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆