I have a very specific question that I need answered for a very specific performance test. I will simplify the code, to make it easier understandable.
我有一个非常具体的问题,我需要回答一个非常具体的性能测试。我将简化代码,使其更容易理解。
I do many calculations (a few ten million), all done in loops that are themselves not too long and I need the total calculation time. However, all the calculations are not done directly one after another and some extra code "comes inbetween" every loop.
我做了很多计算(几千万),都是在循环中完成的,这些循环本身不会太长,我需要总的计算时间。但是,所有的计算都不是一个接一个地直接完成的,而是每个循环都有一些额外的代码“介于两者之间”。
What I need, is quite exactly the total time of all loops (including declaring and initializing i, incrementing it, and comparison with 100 in exmaple). To measure the time I start a Stopwatch
directly before every loop and stop it directly after the loop. But do those two actions need some time that might be relevant?
我需要的是所有循环的总时间(包括声明和初始化i,递增它,并在exmaple中与100进行比较)。为了测量时间,我在每个循环之前直接启动秒表并在循环之后直接停止。但这两项行动是否需要一些可能相关的时间?
Example:
Stopwatch sw = new Stopwatch();
sw.Start();
//how long does it now take until for loop is reached?
for(int i = 0; i < 100; i++)
{
//some calculations
}
//how much time does it take to stop sw?
sw.Stop();
Of course it wouldn't really matter if all the loops were done directly one after another, I could just start at the beginning of the first loop and stop at the end of the last one, but that is not possible here.
当然,如果所有循环一个接一个地直接完成并不重要,我可以从第一个循环的开头开始并在最后一个循环结束时停止,但这不可能在这里。
I know the question is very specific, but I hope someone can help me here, as a significant time coming from around fifty million loops, could in a way affect my results.
我知道这个问题非常具体,但我希望有人可以在这里帮助我,因为大约五千万个循环的重要时间可能会影响我的结果。
2 个解决方案
#1
2
Well, you could try this and check it on your own, on my (lousy performing ) computer it's about 0.0006 milliseconds for the first execution and 0.0001 milliseconds for next executions. Try it yourself:
好吧,你可以尝试这个并自己检查一下,在我的(糟糕表现)计算机上,第一次执行时约为0.0006毫秒,下次执行时约为0.0001毫秒。亲自尝试一下:
Stopwatch sw = new Stopwatch();
sw.Start();
sw.Stop();
TimeSpan ts = TimeSpan.FromTicks(sw.ElapsedTicks);
string elapsed = ts.TotalMilliseconds.ToString();
#2
4
What I need, is quite exactly the total time of all loops. To measure the time I start a StopWatch directly before every loop and stop it directly after the loop. But do those two actions need some time that might be relevant?
我需要的是所有循环的总时间。为了测量时间,我在每个循环之前直接启动一个StopWatch,并在循环之后直接停止它。但这两项行动是否需要一些可能相关的时间?
They take some time - as does the initialization of i
, and the increment, and the comparison with 100 in each loop iteration. Almost nothing is instantaneous in computing.
它们需要一些时间 - 就像i的初始化和增量一样,以及在每次循环迭代中与100的比较。计算中几乎没有什么是瞬间的。
Whether it's relevant or not is another matter. Unless your calculation is utterly trivial, it's unlikely that this will be significant in your timing. You could try timing an empty loop, but it's possible that the JIT compiler will get rid of the loop itself entirely.
是否相关是另一回事。除非你的计算完全无关紧要,否则这在你的时间安排上不太可能。您可以尝试计算一个空循环,但JIT编译器可能会完全摆脱循环本身。
#1
2
Well, you could try this and check it on your own, on my (lousy performing ) computer it's about 0.0006 milliseconds for the first execution and 0.0001 milliseconds for next executions. Try it yourself:
好吧,你可以尝试这个并自己检查一下,在我的(糟糕表现)计算机上,第一次执行时约为0.0006毫秒,下次执行时约为0.0001毫秒。亲自尝试一下:
Stopwatch sw = new Stopwatch();
sw.Start();
sw.Stop();
TimeSpan ts = TimeSpan.FromTicks(sw.ElapsedTicks);
string elapsed = ts.TotalMilliseconds.ToString();
#2
4
What I need, is quite exactly the total time of all loops. To measure the time I start a StopWatch directly before every loop and stop it directly after the loop. But do those two actions need some time that might be relevant?
我需要的是所有循环的总时间。为了测量时间,我在每个循环之前直接启动一个StopWatch,并在循环之后直接停止它。但这两项行动是否需要一些可能相关的时间?
They take some time - as does the initialization of i
, and the increment, and the comparison with 100 in each loop iteration. Almost nothing is instantaneous in computing.
它们需要一些时间 - 就像i的初始化和增量一样,以及在每次循环迭代中与100的比较。计算中几乎没有什么是瞬间的。
Whether it's relevant or not is another matter. Unless your calculation is utterly trivial, it's unlikely that this will be significant in your timing. You could try timing an empty loop, but it's possible that the JIT compiler will get rid of the loop itself entirely.
是否相关是另一回事。除非你的计算完全无关紧要,否则这在你的时间安排上不太可能。您可以尝试计算一个空循环,但JIT编译器可能会完全摆脱循环本身。