C# reflection slower 2.19X+ than direct

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

 

posted @ 2024-08-27 18:38  FredGrit  阅读(2)  评论(0编辑  收藏  举报