[C #] I compared the speeds of Debug.WriteLine and Console.WriteLine.

Caution

Please note that there may be some mess.

What are Debug.WriteLine and Console.WriteLine?

Debug.WriteLine A method that writes information about debugging to the trace listeners in the Listeners collection. You can output Log in the output column when set to Debug in Visual Studio Console.WriteLine I don't think it's necessary to explain A method that outputs data to the console

If you think it's late

Debug.WriteLine is generally said to be slow If you use it too much, it will be quite slow.

Verification

I wanted to compare how slow it was with Console.WriteLine

Method of verification

Each measurement measures the time when 10,000 times are repeated 10 times. Calculate the average value of 10000 times, and then calculate the speed of one time from it. program

using System;
using System.Diagnostics;
using System.Linq;

namespace log_speed
{
    class Program
    {
        static void Main(string[] args)
        {
            long[] TempTime=new long[10];
            double AvDebug,AvConsole;
            
            Stopwatch sw = new Stopwatch();
            for (int i = 0; i < 10; i++)
            {
                sw.Start();
                for (int j = 1; j <= 10000; j++)
                {
                    Debug.WriteLine(j + "Time");
                }
                sw.Stop();
                TempTime[i] = sw.ElapsedMilliseconds;
                sw.Reset();
            }
            AvDebug =TempTime.Average();
            for (int i = 0; i < 10; i++)
            {
                sw.Start();
                for (int j = 1; j <= 10000; j++)
                {
                    Console.WriteLine(j + "Time");
                }
                sw.Stop();
                TempTime[i] = sw.ElapsedMilliseconds;
                sw.Reset();
            }
            AvConsole = TempTime.Average();
            Console.WriteLine("Average speed of Debug 10000 times:" + AvDebug+ "Average speed per Debug:" + AvDebug/10000 + "\nConsole Average speed of 10000 times:" + AvConsole+ "Average speed per Console:" + AvConsole/10000);
            Console.ReadKey();
        }
    }
}

Please contact me if there is something strange

Run

Execution environment

type Maker Part name Quantity
CPU AMD Ryzen 5 1600 1
Motherboard ASUS PRIME B450M-A 1
GPU nVIDIA GTX960 1
RAM G.Skill F4-2400C15-4GNT 4
SSD Samsung 860 EVO M.2 500GB 1

I think there are few bottlenecks in this verification.

IDE is ** Microsoft Visual Studio Community 2019 Version 16.7.6 ** The target framework is **. NET Framework 4.7.2 **

Execution result

The execution result looks like this 結果.png

The average speed of 10000 times is the time required for 10000 times out of 10 times of 10000 times. Seems to be about 3.4 times slower

Summary and impressions

What I thought when comparing this time is that Debug.WriteLine is not so slow, just because the speed is 3.4 times different. I think it's better to use Console.WriteLine as much as possible

Recommended Posts