[20251223-막간] Console 화면 출력 속도

SmartBear·2025년 12월 26일

(막간) 수업시작 전 간단한 테스트

당연히, 당연하지만, 콘솔 프로젝트에서 화면 출력에 대한 부분에 대해 이중 for 문을 활용하여 각각의 item 을 Console.WriteConsole.WriteLine을 활용해 출력하였다.
그런데 여기서든 생각은

이거 함수 call 을 매번 하는 것보다 한방에 찍는게 훨씬 빠르지 않나..?

였고, 이에 대한 테스트를 간략히 해 보았다.

Test Code

복잡하지 않게 간단히 구성하였다.

using System;
using System.Diagnostics;

namespace Examples
{
    internal class Program
    {
        static string[,] ReadyMap()
        {
            string WALL = "🧱";
            string[,] map = new string[20, 20];
            for (int i = 0; i < 20; i++)
            {
                for (int j = 0; j < 20; j++)
                {
                    if (i == 0 || i == map.GetLength(0) - 1 || j == 0 || j == map.GetLength(1) - 1)
                    {
                        map[i, j] = WALL;
                    }
                    else
                    {
                        map[i, j] = "  ";
                    }
                }
            }
            return map;
        }
        
        static void PrintEach()
        {
            string[,] map = ReadyMap();
            Console.Clear();
            for (int i = 0; i < 20; i++)
            {
                for (int j = 0; j < 20; j++)
                {
                    Console.Write(map[i, j]);
                }
                Console.WriteLine();
            }
        }

        static void PrintAtOnce()
        {
            string[,] map = ReadyMap();
            string mapString = "";
            Console.Clear();
            for (int i = 0; i < 20; i++)
            {
                for (int j = 0; j < 20; j++)
                {
                    mapString += map[i, j];
                }
                mapString += "\n";
            }
            Console.WriteLine(mapString);
        }
        
        static void Main(string[] args)
        {
            Console.OutputEncoding = System.Text.Encoding.UTF8;
            
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();
            for (int i = 0; i < 100; i++)
            {
                PrintEach();
            }
            stopwatch.Stop();
            TimeSpan timeSpan001 = stopwatch.Elapsed;
            
            
            stopwatch.Restart();
            stopwatch.Start();
            for (int i = 0; i < 100; i++)
            {
                PrintAtOnce();
            }
            stopwatch.Stop();
            TimeSpan timeSpan002 = stopwatch.Elapsed;

            Console.Clear();
            Console.WriteLine($"함수를 여러번으로 출력의 평균; {timeSpan001.TotalMilliseconds / 100}");
            Console.WriteLine($"함수 출력 1번의 평균; {timeSpan002.TotalMilliseconds / 100}");
        }
    }
}

결과

함수를 여러번으로 출력의 평균; 19.283638                                            
함수 출력 1번의 평균; 0.785587

이게 ms 단위이기 때문에 사실 사람이 눈으로 쫒기는 어려운 단위일 지도 모른다. 하지만 20x20 에서 저정도면 100x100 등은 조금 더 많은 차이가 날 수 있다.
논리적으로 생각하면 당연한 결과. 일지도 모르지만, 이것을 코드로 직접 테스트 해 보고 결과서를 눈으로 보는 것은 중요한 행동이라 생각한다.

profile
Python Dev with Infra -> Game Programmer

0개의 댓글