首页 > 编程语言 >C# reflection slower 2.19X+ than direct

C# reflection slower 2.19X+ than direct

时间:2024-08-27 18:36:29浏览次数:6  
标签:slower reflection C# watch direct bk cost props booksList

using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Text;

namespace ConsoleApp58
{
    internal class Program
    {
        static List<Book> booksList { get; set; }
        static void Main(string[] args)
        {
            InitBooksList();
            TestReflectionTime();
            TestDirectTime();
            TestEqual();
            Console.ReadLine();
        }

        static void TestEqual()
        {
            string reflectStr = File.ReadAllText("ReflectTime.txt");
            string directStr = File.ReadAllText("DirectTime.txt");
            if(string.Equals(reflectStr, directStr))
            {
                Console.WriteLine("Equal!");
            }
            else
            {
                Console.WriteLine("Not Equal!");
            }
        }

        static void TestReflectionTime()
        {
            StringBuilder builder = new StringBuilder();
            if(booksList!=null && booksList.Any())
            {
                Stopwatch watch = new Stopwatch();
                watch.Start();
                var props=typeof(Book).GetProperties();
                int propsCount = props.Count();
                foreach (var bk in booksList)
                {
                    builder.AppendLine($"{props[0].GetValue(bk)},{props[1].GetValue(bk)},{props[2].GetValue(bk)},{props[3].GetValue(bk)}," +
                             $"{props[4].GetValue(bk)},{props[5].GetValue(bk)},{props[6].GetValue(bk)}");
                }
               
                watch.Stop();
                Console.WriteLine($"In TestReflectionTime() Time cost:{watch.ElapsedMilliseconds}");
                File.AppendAllText("ReflectTime.txt", builder.ToString());
            }
        }

        static void TestDirectTime()
        {
            StringBuilder builder = new StringBuilder();
            if(booksList!=null && booksList.Any())
            {
                Stopwatch watch = new Stopwatch();
                watch.Start();
                foreach(var bk in booksList)
                {
                    builder.AppendLine($"{bk.Id},{bk.Author},{bk.ISBN},{bk.Name},{bk.Summary},{bk.Title},{bk.Topic}");
                }
                watch.Stop();
                File.AppendAllText("DirectTime.txt", builder.ToString());
                Console.WriteLine($"In TestDirectTime() Time cost:{watch.ElapsedMilliseconds}");
            }
        }

        private static void InitBooksList()
        {
            booksList = new List<Book>();
            for(int i=0;i<1000000;i++)
            {
                booksList.Add(new Book()
                {
                    Id = i,
                    Author = $"Author_{i + 1}",
                    ISBN = $"ISBN_{i + 1}",
                    Name = $"Name_{i + 1}",
                    Summary = $"Summary_{i + 1}",
                    Title = $"Title_{i + 1}",
                    Topic = $"Topic_{i + 1}"
                });
            }
        }
    }





    public class Book
    {
        public int Id { get; set; }
        public string Author { get; set; }
        public string ISBN { get; set; }
        public string Name { get; set; }
        public string Summary { get; set; }
        public string Title { get; set; }
        public string Topic { get; set; }
    }
}

 

 

When the amout is 1 million,reflection cost 429 milliseconds,while direct cost 184, reflect slower 429/184=2.3+ timers than direct.

 When the amount is 10 million,reflect cost 4100 millseconds, while direct cost 1805,reflection is 4100/1805=2.2X+ timer slow than direct

 

 

When the amout is  100 thousand,reflection cost 57 milliseconds,while direct cost 26, reflect slower 57/26=2.19+ timers than direct.

 

When the amout is  10 thousand,reflection cost 3 milliseconds,while direct cost 1, reflect slower 3/1=3 timers than direct.

 

 

Calculate and statistics as below

 

标签:slower,reflection,C#,watch,direct,bk,cost,props,booksList
From: https://www.cnblogs.com/Fred1987/p/18383309

相关文章

  • C++基础
    目录思维导图:学习内容:1.面向对象 1.1.概念1.2.所谓面向对象编程1.3.面向过程与面向对象2.命名空间2.1为何引入命名空间2.2程序中的标识符2.3 系统提供的命名空间std2.4 自定义命名空间 2.4.1 定义格式2.4.2 多个命名2.4.3 命名空间嵌套定义2......
  • Nuclei:开源漏洞扫描器
    Nuclei拥有灵活的模板系统,可以适应各种安全检查。它可以使用可自定义的模板向多个目标发送请求,确保零误报并实现跨多台主机的快速扫描。它支持多种协议,包括TCP、DNS、HTTP、SSL、文件、Whois、Websocket等。特征模板库:社区支持的模板集合,用于针对性地扫描各种漏洞和攻......
  • HBM芯片PI-SI Cosimulation
    现代电子设计在降低功耗、降低电源电压水平和提高速率方面的趋势要求结合SI-PI共同仿真。需要把PDN的每个部分都应仔细建模,并设计为使I/O驱动器看到的输入阻抗最小化。然后应将PDN与信道模型相结合,通过考虑PDN的影响,如电源引起的抖动和电源导轨上引起的纹波,实现输出眼图的准确......
  • DDR5 Channel SI设计的挑战
    DDR5延续了前几代数据速率不断提高的趋势。数据传输速度在3200至6400MT/s之间。同时将继续像前几代一样使用单端数据线的方式。为了帮助减少由高数据速率引起的信号完整性问题,DRAM端也会考虑加入判决反馈均衡(DFE)来减轻反射、ISI对信号传输的影响。DDR5内存设计挑战尽管DDR5......
  • Capital许可分配策略
    在数字化浪潮席卷全球的今天,软件许可管理已成为企业运营不可或缺的一环。Capital许可分配策略,作为业界领先的解决方案,旨在帮助企业实现智能管理、优化资源,进而提升整体运营效率。一、什么是Capital许可分配策略?Capital许可分配策略是一种基于企业实际需求的软件许可分配方案。它......
  • 判断是否有文件并设置理性,上传到cos
    #判断是否有图片文件cos_file_img_list=[]ifnotimg_href_list:passelse:forimg_urlinimg_href_list:print(img_url)suffix=''file_type=......
  • C# yield keyword relieve congest and consume at the same time with produce
    usingSystem.Threading;namespaceConsoleApp57{internalclassProgram{staticvoidMain(string[]args){PrintNumers();Console.WriteLine("Hello,World!");}staticvoidPrintN......
  • Yololov5+Pyqt5+Opencv 实时城市积水报警系统
    在现代城市生活中,积水问题不仅影响交通和人们的日常生活,还可能对城市基础设施造成潜在的威胁。为了快速、准确地识别和应对积水问题,使用计算机视觉技术进行智能积水检测成为一个重要的解决方案。在这篇博客中,我将带你一步步实现一个基于YOLOv5的积水检测系统,帮助你轻松应对城市......
  • Fins TCP协议理解及C Sharp实现思路
     假设本文中使用到设备的ip地址,用于后续内容的理解:客户端(本机电脑windows系统)IP:192.168.1.101服务端(PLComronCJ2M系列)IP和端口号:192.168.1.10:9600 注意:①本文中的FINSTCP报文都是以16进制(Hex)发送出去的,所以对应的转换也都会转成16进制的形式。②16进制He......
  • cadical调用与改写为多输出
     1. 1.关于APP界面的使用 (1)运行arc==2,且arg[2]为指定参数(非.cnf文件),例如使用参数'-h'$./cadical.exe-husage:cadical[<option>...][<input>[<proof>]]where'<option>'isoneofthefollowingcommonoptions:-hpri......