1

I am analyzing the time efficiency of an algorithm, where the algorithm is ran with a random input array of a set size for however many loops there are tests. I am confused as to why the first loop takes monumentally longer than all following loops (tests).

I am running C# using system.diagnostics' stopwatch class. In the algorithm testing function, the stopwatch starts in the line before and ends the line after with no other code chunks. The debugging result depicted in the console show that the first test always takes much longer than all the rest. My knowledge in computer science is limited so apologies if the answer happens to be a no-brainier, but I was not able to find much useful information when googling "Algorithm time efficiency, why first loop takes longer than all others?".

void RunAlgorithm(int numTests =10,  int arraySize = 9, int max =2, int min = -2)
{
    for(int i = 0; i< numTests; i++)
    {
        int[] A = new int[arraySize];
        for (int j = 0; j < arraySize; j++)
        {
            A[j] = rng.Next(min, max);
        }
        Array.Sort(A);
        //run tests and record time
        timer.Start();
        algorithmResult = Algorithm(A);
        timer.Stop();
        timeTaken= (float)timer.Elapsed.TotalMilliseconds;
        totalTime += timeTaken;
        timer.Reset();
        MockReport(timeTaken);
    }
}

in the output, the very first test took 0.3853 seconds whereas all other tests averaged 0.0015 seconds

Wen Sun
  • 11
  • 1
  • [Related?](https://stackoverflow.com/questions/5589409/c-sharp-jit-compiling-and-net) – ProgrammingLlama Apr 03 '19 at 04:50
  • 2
    It could be the JIT. If you want to benchmark .NET code, then use [BenchmarkDotNet](https://github.com/dotnet/BenchmarkDotNet), which takes care of things like JIT warmup for you. – yaakov Apr 03 '19 at 04:54
  • If [duplicate](https://stackoverflow.com/questions/6417598/why-subsequent-direct-method-call-is-much-faster-than-the-first-call) I selected is not enough make sure to read through results of search https://www.bing.com/search?q=c%23+why+first+run+slower+performance, then [edit] post to clarify why you believe this is different case. – Alexei Levenkov Apr 03 '19 at 05:02
  • Is it possible that the stopwatch is already started before entering the loop? You should reset the stopwatch just before starting a new measurement. – Theodor Zoulias Apr 03 '19 at 05:06
  • Thanks everyone, I think I've gained a better understanding of the nature of 'warming up' method calls. I didn't know how to describe it so sorry for the duplicate question. I think i will try either not counting the first test or looking into how RuntimeHelpers.PrepareMethod works. – Wen Sun Apr 03 '19 at 05:17

0 Answers0