zoukankan      html  css  js  c++  java
  • C#实例化对象的三种方式及性能对比

    前言

    做项目过程中有个需求要实例化两万个对象并添加到List中,这个过程大概需要1min才能加载完(传参较多),于是开启了代码优化之旅,再此记录。
    首先想到的是可能实例化比较耗时,于是开始对每种实例化方式进行测试,过程如下

    实例化方式

    1、用 New 关键字实例化一个类
    2、用 Activator 实例化一个类
    3、用 Assembly 实例化一个类

    代码实现

    测试环境:
    vs2019 .NET Framework 4.7
    Intel Core i7-10510U CPU

    首先定义一个类Person

    public class Person 
    {  
        public Person()
        {
        }
        public Person(string name)
        {
            Name = name;
        }
        public string Name { get; set; }
    }
    

    我们先在无参的构造函数中实例化,每种方式进行十次,每次实例化十万次,代码如下

    static void Main(string[] args)
            {
                Console.WriteLine("实例化对象的耗时比较(单位:毫秒)");
                Console.Write("                    ");
                for (int i = 1; i <= 10; i++)
                    Console.Write("{0:G}", i.ToString().PadLeft(5));
                Console.Write("
    ");
                Console.Write("InstanceByNew".PadRight(20));
                for (int i = 1; i <= 10; i++)
                {
                    Person person = null;
                    Stopwatch watch = new Stopwatch();
                    watch.Start();
                    for (int j = 0; j < 100000; j++)
                        person = new Person();
                    watch.Stop();
                    Console.Write(watch.ElapsedMilliseconds.ToString().PadLeft(5));
                }
                Console.Write("
    ");
                Console.Write("InstanceByActivator".PadRight(20));
                for (int i = 1; i <= 10; i++)
                {
                    Type type = Type.GetType("ConsoleApp1.Person");
                    Person person = null;
    
                    Stopwatch watch = new Stopwatch();
                    watch.Start();
                    for (int j = 0; j < 100000; j++)
                    {
                        object obj = Activator.CreateInstance(type);
                        person = obj as Person;
                    }
                    watch.Stop();
                    Console.Write(watch.ElapsedMilliseconds.ToString().PadLeft(5));
                }
                Console.Write("
    ");
                Console.Write("InstanceByAssembly".PadRight(20));
                for (int i = 1; i <= 10; i++)
                {
                    Assembly assembly = Assembly.Load("InstancePerformance");
                    Person person = null;
    
                    Stopwatch watch = new Stopwatch();
                    watch.Start();
                    for (int j = 0; j < 100000; j++)
                    {
                        object obj = assembly.CreateInstance("ConsoleApp1.Person");
                        person = obj as Person;
                    }
                    watch.Stop();
                    Console.Write(watch.ElapsedMilliseconds.ToString().PadLeft(5));
                }
                Console.Write("
    ");
                Console.ReadKey();
            }
    

    执行结果如下:
    在这里插入图片描述

    然后来看下有参构造函数中实例化的代码和结果

    static void Main(string[] args)
            {
                Console.WriteLine("实例化对象的耗时比较(单位:毫秒)");
                Console.Write("                    ");
                for (int i = 1; i <= 10; i++)
                    Console.Write("{0:G}", i.ToString().PadLeft(5));
                Console.Write("
    ");
                Console.Write("InstanceByNew".PadRight(20));
                for (int i = 1; i <= 10; i++)
                {
                    Person person = null;
                    Stopwatch watch = new Stopwatch();
                    watch.Start();
                    for (int j = 0; j < 100000; j++)
                        person = new Person("Test"+j);
                    watch.Stop();
                    Console.Write(watch.ElapsedMilliseconds.ToString().PadLeft(5));
                }
                Console.Write("
    ");
                Console.Write("InstanceByActivator".PadRight(20));
                for (int i = 1; i <= 10; i++)
                {
                    Type type = Type.GetType("ConsoleApp1.Person");
                    Person person = null;
    
                    Stopwatch watch = new Stopwatch();
                    watch.Start();
                    for (int j = 0; j < 100000; j++)
                    {
                        object obj = Activator.CreateInstance(type,new object[]{"Test"+j});
                        person = obj as Person;
                    }
                    watch.Stop();
                    Console.Write(watch.ElapsedMilliseconds.ToString().PadLeft(5));
                }
                Console.Write("
    ");
                Console.Write("InstanceByAssembly".PadRight(20));
                for (int i = 1; i <= 10; i++)
                {
                    Assembly assembly = Assembly.Load("InstancePerformance");
                    Person person = null;
    
                    Stopwatch watch = new Stopwatch();
                    watch.Start();
                    for (int j = 0; j < 100000; j++)
                    {
                        object obj = assembly.CreateInstance("ConsoleApp1.Person", true, System.Reflection.BindingFlags.Default, null, new []{"Test"+j}, null, null);
                        person = obj as Person;
                    }
                    watch.Stop();
                    Console.Write(watch.ElapsedMilliseconds.ToString().PadLeft(5));
                }
                Console.Write("
    ");
                Console.ReadKey();
            }
    

    执行结果如下:
    在这里插入图片描述

    结论

    从上面的执行结果可以看出这三种方式的性能排序为
    New > Activator > Assembly
    但使用哪种方法还要视情况而定

    后续

    以上可以看出New性能最高,而我就是使用的New,问题还没解决,把问题指向判断List是否存在某元素上,于是开始测试List中的Contains, Exists, Any,Where。详情请点击.

  • 相关阅读:
    css自适应宽高等腰梯形
    控制台屏蔽某console的输出
    js定时器的时间最小值-setTimeout、setInterval
    03_数字的字面量
    程序员-表情包
    程序员-趣图集
    js不是从上到下执行的吗?
    CSS样式重置
    系统程序名命令表
    js手风琴图片切换实现原理及函数分析
  • 原文地址:https://www.cnblogs.com/ziqinchao/p/14116809.html
Copyright © 2011-2022 走看看