如果在C#语句的性能
我只是想确定,当它被用在与大量的迭代周期对我的C#应用程序的性能每一个如果声明的影响。我还没有发现关于这个话题,所以我创建了这一点。
I'm just trying to determine the impact of each "if" statement on the performance of my C# application when it is used in cycles with large number of iterations. I have not found the topic about this so I have created this one.
有关测试我做了2个周期:一个没有如果,一个用一个if语句。在code是如下。
For the test I made 2 cycles: one without "if" and one with a single "if" statement. The code is the following.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
namespace IfPerformance
{
class Program
{
static void Main(string[] args)
{
int N = 500000000;
Stopwatch sw = new Stopwatch();
double a = 0, b = 0;
bool f;
sw.Restart();
for (int i = 0; i < N; i++)
{
a += 1.1;
f = a < N;
}
sw.Stop();
Console.WriteLine("Without if: " + sw.ElapsedMilliseconds + " ms");
a = 0;
sw.Restart();
for (int i = 0; i < N; i++)
{
if (a < N)
a += 1.1;
else
b += 1.1;
}
sw.Stop();
Console.WriteLine("With if: " + sw.ElapsedMilliseconds + " ms");
Console.ReadKey();
}
}
}
我跑了试验用优化code构建选项和不开始调试。结果如下: 如果没有的话:154毫秒 随着如果:742毫秒
I ran the test with "Optimize code" build option and "Start without debugging". The result is the following: Without if: 154 ms With if: 742 ms
这意味着一个if语句带来近5倍增长放缓的表现。我觉得这方面会有所帮助。
This means that a single "if" statement brings almost 5 times slowdown to the performance. I think regarding this will be helpful.
另外,我注意到的presence一些额外的if在一个大的循环可能由25%,这在我看来是显著减慢我的最终应用。
Also, I have noticed that the presence of several extra "if"s in a large loop may slow down my final application by 25%, which on my opinion is significant.
要具体,我在一组数据,需要通过整个数据集多回路的运行蒙特卡洛优化。该循环包含分支这取决于用户的设置。从这个角度的if出现。
To be specific, I'm running Monte-Carlo optimization on a set of data, which require many loops through the whole data set. The loop contains branching which depends on the user settings. From this point "if"s arise.
我的问题在性能方面的专业人士:
My questions to the professionals in performance aspects are:
- 什么是额外的if上运行多个迭代的时间的影响,在一个循环?
- 如何避免衰退?
请发表您的意见,如果我去了错误的方向。
Please post your opinion if I'm going in the wrong direction.
没关系... 您正在测试500百万次迭代......,它需要不到一秒钟......在最坏的情况下...
It doesn't matter ... You're testing 500 MILLION iterations ... and it takes less than a second ... IN THE WORST case ...
正如评论所说,你会在一个麻烦的地狱,首先,因为你不会运行在调试测试性能,即使这样,你就会有其他的事情要考虑堆(它是关于性能测试整个大的世界,它不是那么简单,因为它通常看起来是)。
As comments said, you'll be in a hell of a trouble to begin with, since you won't be running in debug for testing performance, and even then, you'll have heaps of other things to take into consideration (it's a whole big world about performance testing, and it's not as simple as it seems usually).
现在,请注意,您正在做两个不同的东西在两个地方。如果你想看到的,如果表现,就应该让他们做的基本上是相同的。我敢肯定的分支改变IL code开始与...
Now, do notice that you're doing two different things in the two places. If you would like to see the performance of the if, you should have them do basically the same. I'm sure the branching changes the IL code to begin with ...
最后,但并非最不重要的,因为我说了一遍......这DOESTN'T重要...除非你真的需要运行500万次,而这个在很多地方,你的程序开始变慢,因为这一点。
Last, but not least, as I said again ... it DOESTN'T MATTER ... unless you really need to run 500 MILLION times, and have this in so many places that your program starts to slow down because of that.
去了可读性困扰,如果你能够节省一些微秒的if语句
Go for readability over obsessing if you can save some micro seconds on an if statement
随意阅读这些文章埃里克利珀(谁拥有唯一250K代表和是是在C#编译器团队的一名主要开发人员:)的谁去给你在正确的方向:
Feel free to read these articles by Eric Lippert (who has "only" 250K rep and i̶s̶ was a principal developer on the C# compiler team :) who'll get you on the right direction:
- c# performance bencmarks mistakes part 1
- c# performance bencmarks mistakes part 2
- c# performance bencmarks mistakes part 3
- c# performance bencmarks mistakes part 4
(说到这一点,我猜想,垃圾收集(第4条)可能是要考虑的事情...)
(Talking about this, I would guess that garbage collection (article 4) might have been something to consider ...)
然后再看看:的话题这出精心设计的答案
Then look at: this elaborate answer about the topic
最后,但并非最不重要的,看看写更快管理code:知道什么东西花。这是扬灰,从微软CLR性能团队。我会说实话,说我没看过这其中的但的:)。我会虽然,后来就......
And last, but not least, have a look at Writing Faster Managed Code: Know What Things Cost. This is by Jan Gray, from the Microsoft CLR Performance Team. I'll be honest and say I didn't read this one yet :). I Will though, later on...
它接着一...:)