6

I have been looking into speeding up my application as it is performance critical... i.e. every millisecond I can get out of it is better. To do this I have a method that calls some other methods and each of these other methods is wrapped with a Stopwatch timer and Console.WriteLine calls. I.e.:

private void SomeMainMethod()
{
    System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
    sw.Start();
    SomeMethod();
    sw.Stop();
    Console.WriteLine("Time for SomeMethod = {0}ms", sw.ElapsedMilliseconds);

    sw.Reset();
    sw.Start();
    SomeOtherMethod();
    sw.Stop();
    Console.WriteLine("Time for SomeOtherMethod= {0}ms", sw.ElapsedMilliseconds);

    //...
}

The problem is whenever I comment out the Stopwatch and Console.WriteLine lines the code runs about 20ms (not 50) slower which is a lot for what I need.

Does anyone know why this is?

EDIT: The SomeMainMethod method and others in the class are also wrapped in a Stopwatch and Console.WriteLine calls similar to above.

The SomeMainMethod and the methods it calls is part of a class that is part of a Class Library that is called from a console testbed, all of which is single threaded.

For more information: The app is running in x86 .NET 4.6.1 Release mode with optimisations enabled. I am also running this in visual studio 2013 not outside of it.

TheLethalCoder
  • 6,668
  • 6
  • 34
  • 69

1 Answers1

2

After reading a very similar question with no answers I may have found the issue. In the comments section a user (ForguesR) made the following comment:

It is really a big guess : maybe because you are writing to IO your thread gets more processor time because WriteLine is synchronized and thus blocking other threads.

So I wanted to check if this was indeed the case so I changed SomeMainMethod to like the following:

NOTE: It is generally not advised to play around with thread priorities, this was only a workaround to test the theory. I would strongly advise against doing this in production code unless you are 100% sure you know what you are doing. Then probably still stay away from it.

private void SomeMainMethod()
{
    System.Threading.ThreadPriority tp = System.Threading.ThreadPriority.Normal;
    try
    {
        tp = System.Threading.Thread.CurrentThread.Priority;

        System.Threading.Thread.CurrentThread.Priority = System.Threading.ThreadPriority.Highest;

        System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
        sw.Start();
        SomeMethod();
        sw.Stop();
        Console.WriteLine("Time for SomeMethod = {0}ms", sw.ElapsedMilliseconds);

        sw.Reset();
        sw.Start();
        SomeOtherMethod();
        sw.Stop();
        Console.WriteLine("Time for SomeOtherMethod= {0}ms", sw.ElapsedMilliseconds);

        //...
    }
    finally
    {
        System.Threading.Thread.CurrentThread.Priority = tp;
    }
}

After making this change my code now runs consistently faster (~10ms) when the Console and Stopwatch lines are commented out. Therefore I believe his comment was probably correct, at least in my situation.

Community
  • 1
  • 1
TheLethalCoder
  • 6,668
  • 6
  • 34
  • 69
  • Out of interest, what if you were to perform SomeMethod a hundred times. My guess is that CPU throtling is infact the culprit (seemingly cured by Console.Write). Are you in High Performance mode? Balanced mode will take one second to 'spin up' - and it will also be shy of full turbo speeds. And you say 10ms is a significant amount of time for you, but 10ms is easily going to be introduced if the code is run on a slower computer. – Patrick May 25 '16 at 02:08
  • @Patrick My PC is terrible so the code should never be run on anything worse. And I did run the code a lot of times without restarting – TheLethalCoder May 25 '16 at 09:23