时间:2021-05-19
做项目过程中有个需求要实例化两万个对象并添加到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("\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(); }执行结果如下:
然后来看下有参构造函数中实例化的代码和结果
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(); }执行结果如下:
从上面的执行结果可以看出这三种方式的性能排序为
New > Activator > Assembly
但使用哪种方法还要视情况而定
以上可以看出New性能最高,而我就是使用的New,问题还没解决,把问题指向判断List是否存在某元素上,于是开始测试List中的Contains, Exists, Any,Where。详情请点击.
到此这篇关于C#实例化对象的三种方式及性能对比的文章就介绍到这了,更多相关C#实例化对象的三种方式及性能对比内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文实例讲述了JavaHashMap三种循环遍历方式及其性能对比。分享给大家供大家参考,具体如下:HashMap的三种遍历方式(1)foreachmap.ent
本文实例讲述了C#初始化数组的方法。分享给大家供大家参考,具体如下:C#声明数组并初始化,有三种方式。对于一维数组:usingSystem;usingSyste
本文实例讲述了C#实现向多线程传参的三种方式。分享给大家供大家参考,具体如下:从《C#高级编程》了解到给线程传递参数有两种方式,一种方式是使用带Paramete
C#提供三种序列化方式,分别为:1、是使用BinaryFormatter进行串行化;2、使用SoapFormatter进行串行化;3、使用XmlSerializ
javascript面向对象function详解js中的函数有三种表示方式://函数的第一种表示方式:函数关键字的方式functionf1(){alert("f