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?
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.
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: