• 用BenchmarkDotNet看Method


      在前面的文章中看了Property的几种不同访问方式《用BenchmarkDotNet看Property》,性能调用上的差别明显,那同样作为class里重要成员,Method性能如何呢?

    下面是被测试方法

      public class MyClass
        {
            public string MyMethod()
            {
                return DateTime.Now.ToString();
            }
        }

    具体调用方式:实例化调用,反射调用,委托调用

    using BenchmarkDotNet.Attributes;
    using BenchmarkDotNet.Running;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Reflection;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Demo01
    {
        [MemoryDiagnoser]
        class MethodDemo : IDemo
        {
            public void Run()
            {
                BenchmarkRunner.Run<TestMethod>();
            }
        }
        public class TestMethod
        {
            private readonly MyClass _myClass;
            private readonly Func<MyClass, string> _delegate;
            private readonly MethodInfo _methodinfo;
    
            public TestMethod()
            {
                _myClass = new MyClass();
                _methodinfo = _myClass.GetType().GetMethod("MyMethod");
                _delegate = (Func<MyClass, string>)Delegate.CreateDelegate(typeof(Func<MyClass, string>), _methodinfo);
            }
    
            [Benchmark]
            public string MethodA()
            {
                return _myClass.MyMethod();
            }
            [Benchmark]
            public string MethodAExt()
            {
                var myClass = new MyClass();
                return myClass.MyMethod();
            }
            [Benchmark]
            public string MethodB()
            {
                return _methodinfo.Invoke(_myClass, new object[0]).ToString();
            }
            [Benchmark]
            public string MethodBExt()
            {
                var myClass = new MyClass();
                var methodinfo = _myClass.GetType().GetMethod("MyMethod");
                return methodinfo.Invoke(myClass, new object[0]).ToString();
            }
            [Benchmark]
            public string MethodC()
            {
                return _delegate(_myClass);
            }
            [Benchmark]
            public string MethodCExt()
            {
                var myClass = new MyClass();
                var methodinfo = myClass.GetType().GetMethod("MyMethod");
                var dele = (Func<MyClass, string>)Delegate.CreateDelegate(typeof(Func<MyClass, string>), methodinfo);
                return dele(myClass);
            }
        }

      可以看到反射的性能还是相对低的,同样委托实例化的性能也比较低。

     

      想要更快更方便的了解相关知识,可以关注微信公众号 

     

  • 相关阅读:
    内置系统账户:Local system/Network service/Local Service 区别
    python-基于文件导入的特性的单例模式
    php原因 nginx报错[error] 10773#0: *272212065 recv() failed (104: Connection reset by peer) while reading response header from upstream
    实用Django ORM实用操作方法
    session是什么和cookie的区别?
    Python可迭代对象,迭代器,生成器
    浅析python中的GIL锁和协程
    git 常用
    testlink安装
    redmine搭建
  • 原文地址:https://www.cnblogs.com/axzxs2001/p/15872802.html
Copyright © 2020-2023  润新知