결과

# DEBUG
Test00 elased: 60 msec
total count: 10000000
Test01 elased: 17397 msec
total count: 20000000
Test02 elased: 9794 msec
total count: 30000000


# RELEASE
Test00 elased: 11 msec
total count: 10000000
Test01 elased: 17211 msec
total count: 20000000
Test02 elased: 9560 msec
total count: 30000000

 

 

 

코드

using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;

namespace test_attribute
{
    [AttributeUsage(AttributeTargets.Method)]
    public class AttributeCustom : Attribute
    {
        public string Name { get; private set; }

        public AttributeCustom(string myproperty)
        {
            Name = myproperty;
        }
    }

    public class Test
    {
        public int Count { get; private set; }


        public void Test00()
        {
            Count++;
            string name = "name";
        }


        [AttributeCustom("test-01")]
        public void Test01()
        {
            Count++;
            var method = MethodBase.GetCurrentMethod();
            var attr = method.GetCustomAttribute<AttributeCustom>();
            string name = attr.Name;
            //Console.WriteLine($"attr name {attr.Name}");
        }


        [AttributeCustom("test-02")]
        public void Test02()
        {
            Count++;
            string name = GetName();
            //Console.WriteLine($"attr name: {GetName()}");
        }


        public string GetName([CallerMemberName] string methodName = "")
        {
            //Console.WriteLine($"{methodName}");
            var attr = GetType().GetMethod(methodName).GetCustomAttribute<AttributeCustom>();
            return attr.Name;
        }
    }

    public class Program
    {

        /*
            # DEBUG
            Test00 elased: 60 msec
            total count: 10000000
            Test01 elased: 17397 msec
            total count: 20000000
            Test02 elased: 9794 msec
            total count: 30000000


            # RELEASE
            Test00 elased: 11 msec
            total count: 10000000
            Test01 elased: 17211 msec
            total count: 20000000
            Test02 elased: 9560 msec
            total count: 30000000
         */
        static void Main(string[] _)
        {
            Test test = new Test();

            int count = 10_000_000;

            Stopwatch sw = new Stopwatch();

            sw.Reset();
            sw.Start();
            for (int i = 0; i < count; i++)
            {
                test.Test00();
            }
            Console.WriteLine($"Test00 elased: {sw.ElapsedMilliseconds} msec");
            Console.WriteLine($"total count: {test.Count}");

            sw.Reset();
            sw.Start();
            for (int i = 0; i < count; i++)
            {
                test.Test01();
            }
            Console.WriteLine($"Test01 elased: {sw.ElapsedMilliseconds} msec");
            Console.WriteLine($"total count: {test.Count}");

            sw.Reset();
            sw.Start();
            for (int i = 0; i < count; i++)
            {
                test.Test02();
            }
            Console.WriteLine($"Test02 elased: {sw.ElapsedMilliseconds} msec");
            Console.WriteLine($"total count: {test.Count}");
        }

    }
}

 

 

 

'C#' 카테고리의 다른 글

c# DebuggerDisplay, DebuggerTypeProxy attribute  (0) 2022.03.16
c# HttpClient  (0) 2022.03.15
c# channel  (0) 2022.03.15
object pool, object type pool  (0) 2022.03.11
GC TEST  (0) 2022.03.08
GC EventPipe 모니터링  (0) 2022.03.08
C# .net core 빌드 및 powershell 전송  (0) 2022.03.01
c# stack size 확인  (0) 2022.02.24