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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

以非泛型方式调用泛型方法(三)

發布時間:2024/1/17 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 以非泛型方式调用泛型方法(三) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
結論:
以下是測試代碼:
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?成員
????}

}

測試結果:
方案耗時比對其他優點
直接調用181不通用
泛型委托包裝432.39不通用
反射16538918.78通用,不需額外定義
非泛型接口包裝603.33通用,需要額外定義并實現
動態生成的非泛型接口包裝724通用,需要額外定義
版權聲明:原創作品,如需轉載,請注明出處。否則將追究法律責任 本文轉自 obarton 51CTO博客,原文鏈接:http://blog.51cto.com/kanas/285945

總結

以上是生活随笔為你收集整理的以非泛型方式调用泛型方法(三)的全部內容,希望文章能夠幫你解決所遇到的問題。

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