Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Console.WriteLine speeds up my code?

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.

like image 743
TheLethalCoder Avatar asked May 09 '16 15:05

TheLethalCoder


People also ask

What does console WriteLine do?

These C# example programs show the Console.WriteLine method. Console.WriteLine has many overloads. Console.WriteLine renders a line of text on the console.

How do I write a line in console?

It is helpful to know that when you use the Console class, you do not need to create a new Console (). You need to add the "using System" line at the start. WriteLine is a static method on the Console type. Alternatively: You can specify System.Console.WriteLine () to reference the namespace directly. Empty line.

How to write multiple values on a single line in console?

Console.WriteLine ( "B" ); } } Output A B Format strings. In some programs, you will want to write several values on a single line to the Console. One way you can do this is by appending the string using the + operator before passing it to Console.WriteLine. It is often clearer to use a format string.

How to stop the screen when console Readline is running?

Please remove Console.Readline () or use System.Console.ReadLine (); to halt the screen. Now all your console.Witeline output will be executed as debug.WriteLine and console output visible in Output window.


1 Answers

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.

like image 156
TheLethalCoder Avatar answered Nov 10 '22 00:11

TheLethalCoder