日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Class 创建性能大比拼(反射,泛型反射,泛型创建,缓存Emit,非缓存Emit)

發布時間:2023/12/10 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Class 创建性能大比拼(反射,泛型反射,泛型创建,缓存Emit,非缓存Emit) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

  一說到反射,很多人都想到了性能,更有甚者直接說“慎用反射,遺患無窮”,“用反射,感覺怎么像是退步啊~”,看到這種言論,直接把反射妖魔化了,如果這種言論長此以往,勢必會對很多對反射初學者造成負面影響。反射是一把雙刃劍,看你怎樣使用了,下面我就用代碼說話。

class TestEntity { }

  1. 手工創建TestEntity?

[TestInfo(Category = "Class.Constructor", Name = "Direct")]
class DirectInvokeMode:IRunable
{
public void Run()
{
new TestEntity();
}
}

?? ? 2. 反射創建TestEntity?

[TestInfo(Category = "Class.Constructor", Name = "Reflect")]
class ReflectInvokeMode : IRunable
{
public void Run()
{
Activator.CreateInstance(
typeof(TestEntity));
}
}

  3. 泛型反射創建TestEntity

[TestInfo(Category = "Class.Constructor", Name = "GenericReflect")]
class GenericReflectInvokeMode : IRunable
{
public void Run()
{
Activator.CreateInstance
<TestEntity>();
}
}

  4. Generic 直接創建

[TestInfo(Category = "Class.Constructor", Name = "Generic Create")]
class GenericCreateInvokeMode : IRunable
{
public void Run()
{
Create
<TestEntity>();
}

static T Create<T>() where T : new()
{
return new T();
}
}
5. Emit創建 [TestInfo(Category = "Class.Constructor", Name = "Emit")]
class EmitInvokeMode : IRunable
{
static readonly ConstructorHandler Ctor = typeof(TestEntity).GetConstructor(Type.EmptyTypes).GetCreator();

public void Run()
{
Ctor();
}
}

  6. 不緩存Emit的創建

[TestInfo(Category = "Class.Constructor", Name = "NoCacheEmit")]
class NoCacheEmitInvokeMode : IRunable
{
public void Run()
{
typeof(TestEntity).GetConstructor(Type.EmptyTypes).GetCreator()();
}
}

  測試程序:(執行10萬次創建Class對象)

foreach (var item in Mappers)
CodeTimer.Time(item.Metadata.Category
+ "->" + item.Metadata.Name, 100000, () => item.Value.Run());

輸出結果:

------ Test started: Assembly: NLite.Test.dll ------


Class.Constructor
->Direct
Time Elapsed: 5ms
CPU Cycles:
0
Gen
0: 1
Gen
1: 0
Gen
2: 0

Class.Constructor
->Reflect
Time Elapsed: 320ms
CPU Cycles:
2,968,750
Gen
0: 1
Gen
1: 0
Gen
2: 0

Class.Constructor
->GenericReflect
Time Elapsed: 147ms
CPU Cycles:
1,250,000
Gen
0: 1
Gen
1: 0
Gen
2: 0

Class.Constructor
->Generic Create
Time Elapsed: 159ms
CPU Cycles:
1,406,250
Gen
0: 1
Gen
1: 0
Gen
2: 0

Class.Constructor
->Emit
Time Elapsed: 6ms
CPU Cycles:
0
Gen
0: 2
Gen
1: 0
Gen
2: 0

Class.Constructor
->NoCacheEmit
Time Elapsed:
12,786ms
CPU Cycles:
119,218,750
Gen
0: 162
Gen
1: 81
Gen
2: 0

1 passed, 0 failed, 0 skipped, took 67.77 seconds (NUnit 2.5.5).

  性能比較結果應該是一目了然了!

?? ? ?附上源代碼:

using System; using System.Collections.Generic; using System.Linq; using System.Text; using NUnit.Framework; using System.Reflection; using NLite.Reflection;namespace NLite.Test.Reflection {[Contract]public interface IRunable{void Run();}//測試器元數據public interface ITestInfo{//目錄string Category { get; }//名稱string Name { get; }}//映射器元數據注解[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)][MetadataAttributeAttribute]public class TestInfoAttribute : ComponentAttribute{public string Category { get; set; }public string Name { get; set; }}[TestFixture]public class SpecBase{public SpecBase(){}[SetUp]public void SetUp(){Given();When();}public virtual void Given() { }public virtual void When(){}[Test]public void Test(){}}public abstract class PerformanceSpecBase : SpecBase{[InjectMany]protected Lazy<IRunable, ITestInfo>[] Mappers;protected abstract void RegisterComponents();public virtual int Times{get { return 100000; }}public override void Given(){RegisterComponents();ServiceRegistry.Compose(this);}public override void When(){for (int i = 0; i < 3; i++){foreach (var item in Mappers)CodeTimer.Time(item.Metadata.Category + "->" + item.Metadata.Name, Times, () => item.Value.Run());}}}public class InvokeConstructorPerformanceSpec : PerformanceSpecBase{class TestEntity { }protected override void RegisterComponents(){ServiceRegistry.Register<DirectInvokeMode>().Register<ReflectInvokeMode>().Register<GenericReflectInvokeMode>().Register <GenericCreateInvokeMode>().Register<EmitInvokeMode>().Register < NoCacheEmitInvokeMode>().Register < GenericReflectInvokeMode2>();}[TestInfo(Category = "Class.Constructor", Name = "Direct")]class DirectInvokeMode:IRunable{public void Run(){new TestEntity();}}[TestInfo(Category = "Class.Constructor", Name = "Reflect")]class ReflectInvokeMode : IRunable{public void Run(){Activator.CreateInstance(typeof(TestEntity));}}[TestInfo(Category = "Class.Constructor", Name = "GenericReflect")]class GenericReflectInvokeMode : IRunable{public void Run(){Activator.CreateInstance<TestEntity>();}}[TestInfo(Category = "Class.Constructor", Name = "Reflect->Reflect")]class GenericReflectInvokeMode2 : IRunable{static readonly MethodInfo CreateMethod = typeof(Activator).GetMethod("CreateInstance", Type.EmptyTypes).MakeGenericMethod(typeof(TestEntity));public void Run(){CreateMethod.Invoke(null,null);}}[TestInfo(Category = "Class.Constructor", Name = "Generic Create")]class GenericCreateInvokeMode : IRunable{public void Run(){Create<TestEntity>();}static T Create<T>() where T : new(){return new T();}}[TestInfo(Category = "Class.Constructor", Name = "Emit")]class EmitInvokeMode : IRunable{static readonly ConstructorHandler Ctor = typeof(TestEntity).GetConstructor(Type.EmptyTypes).GetCreator();public void Run(){Ctor();}}[TestInfo(Category = "Class.Constructor", Name = "NoCacheEmit")]class NoCacheEmitInvokeMode : IRunable{public void Run(){typeof(TestEntity).GetConstructor(Type.EmptyTypes).GetCreator()();}}}}

?? ? ?最后給大家一個思考題:Struct 創建性能大比拼的結果是怎樣的?(注意Struct是值類型,Class是引用類型,值類型是分配在棧上的,引用類型是分配在堆上的)

轉載于:https://www.cnblogs.com/netcasewqs/archive/2011/04/18/2019999.html

總結

以上是生活随笔為你收集整理的Class 创建性能大比拼(反射,泛型反射,泛型创建,缓存Emit,非缓存Emit)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。