前言

做项目过程中有个需求要实例化两万个对象并添加到List

实例化方式

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

代码实现

首先定义一个类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("\n");
            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("\n");
            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("\n");
            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("\n");
            Console.ReadKey();
        }

执行结果如下:
C#实例化对象的三种方式及性能对比-LMLPHP

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

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("\n");
            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("\n");
            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("\n");
            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("\n");
            Console.ReadKey();
        }

执行结果如下:
C#实例化对象的三种方式及性能对比-LMLPHP

结论

从上面的执行结果可以看出这三种方式的性能排序为

但使用哪种方法还要视情况而定

后续

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

12-11 03:13