1. 程式人生 > >類拷貝效率測試

類拷貝效率測試

現在做的專案原本是WCF+EF的,原本是把EF的類直接給WCF用的。現在需要分開,以便WCF類和EF類分別獨立演化,因為前端和後端的人員不同,類設計習慣不同;而且有時候為了資料庫表的設計,EF類無法直接給前端用。先不論這部分的設計思路對不對,在服務介面部分需要WCF類和EF類轉換,因為基本是相同的類物件,除了一部分屬性不同外,基本上屬性是都相同的,就考慮用反射統一轉換,當然,後續演化屬性名很多不同的話,就會有問題。

總之,我要測試一下用反射動態拷貝和程式碼寫死靜態拷貝的效率差別,不用測試肯定也知道反射效率低,只是想看看差別多少。

兩個類:

LocationCard,EF類,MVC中在頁面上編輯的類

    public class LocationCard:ITag
    {
        /// <summary>
        /// 主鍵Id
        /// </summary>
        [Display(Name = "主鍵Id")]
        public int Id { get; set; }

        /// <summary>
        /// 對接Id
        /// </summary>
        [Display(Name = "對接Id")]
        public int? Abutment_Id { get; set; }

        /// <summary>
        /// 終端編號
        /// </summary>
        [Display(Name = "終端編號")]
        [Required]
        public string Code { get; set; }

        /// <summary>
        /// 終端名稱
        /// </summary>
        [Display(Name = "終端名稱")]
        [Required]
        public string Name { get; set; }

        /// <summary>
        /// 描述
        /// </summary>
        [Display(Name = "描述")]
        public string Describe { get; set; }

        public override string ToString()
        {
            return Name;
        }
    }

Tag WCF類 傳輸給前端用的類物件

    public class Tag: ITag
    {
        public int Id { get; set; }

        public string Code { get; set; }

        public string Name { get; set; }

        public string Describe { get; set; }

        public override string ToString()
        {
            return Name;
        }
    }

ITag的內容就是Tag中的那些屬性

反射方式轉換:

public static List<T2> ConvertObjectList<T1, T2>(this IList<T1> list) where T2 : class, new()
        {
            if (list == null) return null;
            Stopwatch stopwatch=new Stopwatch();
            stopwatch.Start();

            Type type1 = typeof(T1);
            Type type2 = typeof(T2);
            
            //PropertyInfo[] propertyInfos1 = type1.GetProperties();
            PropertyInfo[] propertyInfos2 = type2.GetProperties();

            List<T2> listNew = new List<T2>();
            foreach (T1 item in list)
            {
                //T2 itemNew = ConvertObjectTo<T1, T2>(item);
                //listNew.Add(itemNew);

                T2 obj2 = new T2();
                try
                {
                    foreach (PropertyInfo p2 in propertyInfos2)
                    {
                        try
                        {
                            PropertyInfo p1 = type1.GetProperty(p2.Name);
                            if (p1 == null) continue;
                            object value = p1.GetValue(item);
                            p2.SetValue(obj2, value);
                        }
                        catch (Exception ex)
                        {
                            LogEvent.Info(ex.ToString());
                        }

                    }
                    listNew.Add(obj2);
                }
                catch (Exception ex)
                {
                    LogEvent.Info(ex.ToString());
                }
            }
            stopwatch.Stop();
            LogEvent.Info("ConvertObjectList Time:" + stopwatch.Elapsed);
            return listNew;
        }

靜態程式碼方式轉換

        #region LocationCard <=> Tag
        public static List<Tag> ToWcfModelList(this List<LocationCard> list1)
        {
            return list1.ToTModel().ToWCFList();
        }

        public static List<Tag> ToTModel(this List<LocationCard> list1)
        {
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();

            List<Tag> list2 = new List<Tag>();
            foreach (var item1 in list1)
            {
                list2.Add(item1.ToModel());
            }

            stopwatch.Stop();
            LogEvent.Info("ToTModel Of List Time:" + stopwatch.Elapsed);
            return list2;
        }

        public static Tag ToModel(this LocationCard item1)
        {
            Tag item2 = new Tag();
            item2.Id = item1.Id;
            item2.Name = item1.Name;
            item2.Code = item1.Code;
            item2.Describe = item1.Describe;
           
            return item2;
        }

        public static List<LocationCard> ToDbModel(this List<Tag> list1)
        {
            //return list1.ConvertObjectList<Tag, LocationCard>();
            var list2 = new List<LocationCard>();
            foreach (var item1 in list1)
            {
                list2.Add(item1.ToDbModel());
            }
            return list2;
        }

        public static LocationCard ToDbModel(this Tag item1)
        {
            LocationCard item2 = new LocationCard();
            item2.Id = item1.Id;
            item2.Name = item1.Name;
            item2.Code = item1.Code;
            item2.Describe = item1.Describe;
            return item2;
        }
        #endregion

測試程式碼

class Program
    {
        static void Main(string[] args)
        {
            string input = "";

            Console.WriteLine("Input Count:");
            input = Console.ReadLine();
            while (input.ToLower() != "q") 
            {
                int count = int.Parse(input);
                for (int i = 1; i < 10; i++)
                {
                    TestByCount(count*i);
                }

                Console.WriteLine("Input Count:");
                input = Console.ReadLine();
            } 

            Console.WriteLine("Exit");
            Console.Read();
        }

        private static void TestByCount(int count)
        {
            Console.WriteLine("Count:" + count);
            List<LocationCard> cards = InitList(count);
            List<Tag> tags = cards.ConvertObjectList<LocationCard, Tag>();
            List<Tag> tags2 = cards.ToTModel();
            Console.WriteLine();
        }

        private static List<LocationCard> InitList(int count)
        {
            List<LocationCard> cards = new List<LocationCard>();

            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();

            for (int i = 0; i < count; i++)
            {
                LocationCard card = new LocationCard();
                card.Id = i;
                card.Name = "Card" + i;
                card.Code = "Code" + i;
                card.Abutment_Id = i;
                card.Describe = "Describe" + i;

                //card.Id = i;
                //card.Name = "Card";
                //card.Code = "Code";
                //card.Abutment_Id = i;
                //card.Describe = "Describe";
                cards.Add(card);
            }
            stopwatch.Stop();
            LogEvent.Info("InitList Time:" + stopwatch.Elapsed);
            return cards;
        }
    }

測試結果: