以非泛型方式调用泛型方法(三)
生活随笔
收集整理的這篇文章主要介紹了
以非泛型方式调用泛型方法(三)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
結(jié)論:
以下是測試代碼:
using?System;
using?System.Collections.Generic;
using?System.Reflection;
using?System.Text;
namespace?GenericMethodTest
{
????//?為泛型方法定義的委托
????public?delegate?void?GM<T>(T?obj,?IList<T>?list);
????//?為非泛型方法定義的接口
????public?interface?ING
????{
????????void?NGM(object?obj,?object?list);
????}
????class?Program
????{
????????static?void?Main(string[]?args)
????????{
????????????List<int>?list?=?new?List<int>();
????????????System.Diagnostics.Stopwatch?watch?=?new?System.Diagnostics.Stopwatch();
????????????watch.Reset();
????????????watch.Start();
????????????for?(int?i?=?0;?i?<?1000000;?i++)
????????????{
????????????????list.Add(i);
????????????}
????????????watch.Stop();
????????????long?l1?=?watch.ElapsedMilliseconds;
????????????watch.Reset();
????????????watch.Start();
????????????GM<int>?gm?=?new?GM<int>(Program.Add);
????????????for?(int?i?=?0;?i?<?1000000;?i++)
????????????{
????????????????gm(i,?list);
????????????}
????????????watch.Stop();
????????????long?l2?=?watch.ElapsedMilliseconds;
????????????watch.Reset();
????????????watch.Start();
????????????MethodInfo?mi?=?typeof(Program).GetMethod("Add");
????????????MethodInfo?gmi?=?mi.MakeGenericMethod(typeof(int));
????????????for?(int?i?=?0;?i?<?1000000;?i++)
????????????{
????????????????gmi.Invoke(null,?new?object[]?{?i,?list?});
????????????}
????????????watch.Stop();
????????????long?l3?=?watch.ElapsedMilliseconds;
????????????watch.Reset();
????????????watch.Start();
????????????ING?ng1?=?GetNGC(typeof(int),?typeof(Program),?"Add");
????????????for?(int?i?=?0;?i?<?1000000;?i++)
????????????{
????????????????ng1.NGM(i,?list);
????????????}
????????????watch.Stop();
????????????long?l4?=?watch.ElapsedMilliseconds;
????????????watch.Reset();
????????????watch.Start();
????????????ING?ng2?=?InterfaceGenerator.GetInterface<ING>(new?GM<int>(Program.Add));
????????????for?(int?i?=?0;?i?<?1000000;?i++)
????????????{
????????????????ng2.NGM(i,?list);
????????????}
????????????watch.Stop();
????????????long?l5?=?watch.ElapsedMilliseconds;
????????????Console.WriteLine("{0}\n{1}?vs?{2}?vs?{3}?vs?{4}?vs?{5}",?list.Count,?l1,?l2,?l3,?l4,?l5);
????????????Console.ReadLine();
????????}
????????public?static?void?Add<T>(T?obj,?IList<T>?list)
????????{
????????????list.Add(obj);
????????}
????????static?ING?GetNGC(Type?genericType,?Type?methodType,?string?methodName)
????????{
????????????MethodInfo?mi?=?methodType.GetMethod(methodName);
????????????MethodInfo?gmi?=?mi.MakeGenericMethod(genericType);
????????????Delegate?gmd?=?Delegate.CreateDelegate(typeof(GM<>).MakeGenericType(genericType),?gmi);
????????????return?Activator.CreateInstance(typeof(GClass<>).MakeGenericType(genericType),?gmd)?as?ING;
????????}
????}
????public?class?GClass<T>?:?ING
????{
????????private?GM<T>?m_gmd;
????????public?GClass(GM<T>?gmd)
????????{
????????????m_gmd?=?gmd;
????????}
????????INGClass?成員
????}
}
測試結(jié)果:
版權(quán)聲明:原創(chuàng)作品,如需轉(zhuǎn)載,請注明出處。否則將追究法律責(zé)任 本文轉(zhuǎn)自 obarton 51CTO博客,原文鏈接:http://blog.51cto.com/kanas/285945
以下是測試代碼:
using?System;
using?System.Collections.Generic;
using?System.Reflection;
using?System.Text;
namespace?GenericMethodTest
{
????//?為泛型方法定義的委托
????public?delegate?void?GM<T>(T?obj,?IList<T>?list);
????//?為非泛型方法定義的接口
????public?interface?ING
????{
????????void?NGM(object?obj,?object?list);
????}
????class?Program
????{
????????static?void?Main(string[]?args)
????????{
????????????List<int>?list?=?new?List<int>();
????????????System.Diagnostics.Stopwatch?watch?=?new?System.Diagnostics.Stopwatch();
????????????watch.Reset();
????????????watch.Start();
????????????for?(int?i?=?0;?i?<?1000000;?i++)
????????????{
????????????????list.Add(i);
????????????}
????????????watch.Stop();
????????????long?l1?=?watch.ElapsedMilliseconds;
????????????watch.Reset();
????????????watch.Start();
????????????GM<int>?gm?=?new?GM<int>(Program.Add);
????????????for?(int?i?=?0;?i?<?1000000;?i++)
????????????{
????????????????gm(i,?list);
????????????}
????????????watch.Stop();
????????????long?l2?=?watch.ElapsedMilliseconds;
????????????watch.Reset();
????????????watch.Start();
????????????MethodInfo?mi?=?typeof(Program).GetMethod("Add");
????????????MethodInfo?gmi?=?mi.MakeGenericMethod(typeof(int));
????????????for?(int?i?=?0;?i?<?1000000;?i++)
????????????{
????????????????gmi.Invoke(null,?new?object[]?{?i,?list?});
????????????}
????????????watch.Stop();
????????????long?l3?=?watch.ElapsedMilliseconds;
????????????watch.Reset();
????????????watch.Start();
????????????ING?ng1?=?GetNGC(typeof(int),?typeof(Program),?"Add");
????????????for?(int?i?=?0;?i?<?1000000;?i++)
????????????{
????????????????ng1.NGM(i,?list);
????????????}
????????????watch.Stop();
????????????long?l4?=?watch.ElapsedMilliseconds;
????????????watch.Reset();
????????????watch.Start();
????????????ING?ng2?=?InterfaceGenerator.GetInterface<ING>(new?GM<int>(Program.Add));
????????????for?(int?i?=?0;?i?<?1000000;?i++)
????????????{
????????????????ng2.NGM(i,?list);
????????????}
????????????watch.Stop();
????????????long?l5?=?watch.ElapsedMilliseconds;
????????????Console.WriteLine("{0}\n{1}?vs?{2}?vs?{3}?vs?{4}?vs?{5}",?list.Count,?l1,?l2,?l3,?l4,?l5);
????????????Console.ReadLine();
????????}
????????public?static?void?Add<T>(T?obj,?IList<T>?list)
????????{
????????????list.Add(obj);
????????}
????????static?ING?GetNGC(Type?genericType,?Type?methodType,?string?methodName)
????????{
????????????MethodInfo?mi?=?methodType.GetMethod(methodName);
????????????MethodInfo?gmi?=?mi.MakeGenericMethod(genericType);
????????????Delegate?gmd?=?Delegate.CreateDelegate(typeof(GM<>).MakeGenericType(genericType),?gmi);
????????????return?Activator.CreateInstance(typeof(GClass<>).MakeGenericType(genericType),?gmd)?as?ING;
????????}
????}
????public?class?GClass<T>?:?ING
????{
????????private?GM<T>?m_gmd;
????????public?GClass(GM<T>?gmd)
????????{
????????????m_gmd?=?gmd;
????????}
????????INGClass?成員
????}
}
測試結(jié)果:
| 方案 | 耗時 | 比對 | 其他優(yōu)點 |
| 直接調(diào)用 | 18 | 1 | 不通用 |
| 泛型委托包裝 | 43 | 2.39 | 不通用 |
| 反射 | 16538 | 918.78 | 通用,不需額外定義 |
| 非泛型接口包裝 | 60 | 3.33 | 通用,需要額外定義并實現(xiàn) |
| 動態(tài)生成的非泛型接口包裝 | 72 | 4 | 通用,需要額外定義 |
總結(jié)
以上是生活随笔為你收集整理的以非泛型方式调用泛型方法(三)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 两种驱动系统运行的方式--分时的方式
- 下一篇: LVS DR模式搭建、keepalive