[C# 벤치마크] BenchMarkDotNet IntroBasic

소개

  • 안녕하세요. 오늘은 BenchMarkDotNet에 대해서 학습해 보려고 합니다.
  • 그 중에서도, IntroBasic 관련 내용에 대해서 알아 보도록 하겠습니다.


IntroBasic

  • 측정을 원하는 Method에 [BenchMark] Attribute를 추가합니다.
  • Description을 사용하여 원하는 이름으로 지정하여 출력할 수 있습니다.
  • BenchMarkRunner를 이용하여 원하는 Class의 성능을 측정할 수 있습니다.


예제코드

using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Diagnosers;
using BenchmarkDotNet.Running;
using System.Collections.Generic;

namespace RegexTest
{
    class Program
    {
        static void Main(string[] args)
        {
            var summary = BenchmarkRunner.Run<IntroBasic>();
        }
    }

    [MemoryDiagnoser]
    public class IntroBasic
    {
        public List<int> AddList()
        {
            List<int> list = new List<int>();

            for(int index = 0; index < 10000000; index++)
            {
                list.Add(index);
            }

            return list; 
        }

        [Benchmark]
        public int ForMethod()
        {
            int sum = 0;

            List<int> list = AddList();

            for(int index = 0; index < list.Count; index++)
            {
                sum += list[index];
            }

            return sum;
        }

        [Benchmark(Description ="Foreach 구문 테스트")]
        public int ForeachMethod()
        {
            int sum = 0;

            List<int> list = AddList();

            foreach (int item in list)
            {
                sum += item;
            }

            return sum;
        }
    }
}


실행 결과

  • 위와 같이 Description에 적은 내용이 메서드 이름으로 나오는 것을 확인할 수 있습니다.

728x90

이 글을 공유하기

댓글

Designed by JB FACTORY