惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
SegmentFault 最新的问题
J
Java Code Geeks
V
V2EX
Blog — PlanetScale
Blog — PlanetScale
博客园 - 司徒正美
Hugging Face - Blog
Hugging Face - Blog
F
Fortinet All Blogs
aimingoo的专栏
aimingoo的专栏
B
Blog
A
About on SuperTechFans
有赞技术团队
有赞技术团队
月光博客
月光博客
Microsoft Azure Blog
Microsoft Azure Blog
阮一峰的网络日志
阮一峰的网络日志
腾讯CDC
美团技术团队
大猫的无限游戏
大猫的无限游戏
爱范儿
爱范儿
N
Netflix TechBlog - Medium
C
Check Point Blog
Recent Announcements
Recent Announcements
博客园 - Franky
博客园 - 叶小钗
T
Tailwind CSS Blog

博客园 - 空空儿

硬盘分区、寻址和系统启动过程 JavaScript里String.Format方法的实现 asp.net异步页 获取SQL Server数据行的物理地址信息(%%lockress%% & %%physloc%%) 利用Spire.DataExport将数据(Database/DataTable)导成各种文件 使用XML的value()方法将多行数据合并成一列 获取所有的外键信息 ASP.NET application and page life cycle SQL Server 2005 CLR 调用Web Service需要注意的几个问题 SQL SERVRE 2005 CLR TVF错误:从用户定义的表值函数获取新行时出错:Data access is not allowed in this context. PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 在C#里使用属性 - 空空儿 - 博客园 Read text file (txt, csv, log, tab, fixed length) 批量写数据---将XML数据批量写入数据库 过程 sp_addlinkedsrvlogin,第 91 行解密过程中出错的解决办法 生成大量随机字符串不同实现方式的效率对比 用SQL SERVER 2005新提供的命令实现行列转换 APM (异步编程模型) 利用宏让ERStudio生成代码文件
对对象类型和调用方法属性进行存储以提升反射性能
空空儿 · 2008-04-25 · via 博客园 - 空空儿

反射的性能差是一个公认的事实.而最耗性能的还是根据程序集获取要调用的对象,而在对象里搜索要调用的方法所耗性能到不不是很多,如果对象里的方法不是特别的多,而去可以指定相关参数提高搜索的效率,比如BindingFlags,Binder.
如果将对象的类型和方法属性进行一下存储,性能方法应该会得到改观.
简单的做了下测试:

    class Program
    {
        
static IDictionary<string , Type> typeList = new Dictionary<string , Type>();static IDictionary<string , MethodInfo> methodList = new Dictionary<string , MethodInfo>();static void Main ( string[] args )
        {
            
int iBegin = Environment.TickCount;
            
for ( int i = 0 ; i < 1000 ; i++ ) 
            {
                Test1( 
"Test.TestClass,Test" );
                Test1( 
"Test1.Class1,Test1" );  
            }
            Console.WriteLine( 
"普通:{0}" , Environment.TickCount - iBegin );
            
            GC.Collect();
            iBegin 
= Environment.TickCount;
            
for ( int i = 0 ; i < 1000 ; i++ )
            {
                Test2( 
"Test.TestClass,Test" );     
                Test2( 
"Test1.Class1,Test1" );
            }
            Console.WriteLine( 
"存储Type:{0}" , Environment.TickCount - iBegin );
            GC.Collect();

            iBegin 

= Environment.TickCount;
            
for ( int i = 0 ; i < 1000 ; i++ )
            {
                Test3( 
"Test.TestClass,Test" );
                Test3( 
"Test1.Class1,Test1" );
            }
            Console.WriteLine( 
"存储MethodInfo:{0}" , Environment.TickCount - iBegin );
            GC.Collect();

            Console.ReadLine();
        }

static void Test1 ( string typeName )
        {
            Type type 
= Type.GetType( typeName );
            
object instance = Activator.CreateInstance( type , 1 );
            MethodInfo methodInfo 
= type.GetMethod( "GetValue" );
            methodInfo.Invoke( instance , 
null );
            
            instance 
= Activator.CreateInstance( type , null );
            methodInfo 
= type.GetMethod( "Add" , new Type[] { typeofint ) , typeofint ) } );
            methodInfo.Invoke( instance , 
new object[] { 1 , 2 } );
        }
//存储Type
        static void Test2 ( string typeName )
        {
            Type type 
= GetType( typeName );object instance = Activator.CreateInstance( type , 1 );
            MethodInfo methodInfo 
= type.GetMethod( "GetValue" );
            methodInfo.Invoke( instance , 
null );

            instance 

= Activator.CreateInstance( type , null );
            methodInfo 
= type.GetMethod( "Add" , new Type[] { typeofint ) , typeofint ) } );
            methodInfo.Invoke( instance , 
new object[] { 1 , 2 } );
        }
//存储MethodInfo
        static void Test3 ( string typeName )
        {
            Type type 
= GetType( typeName );
            
object instance = Activator.CreateInstance( type , 1 );
            MethodInfo methodInfo 
= GetMethod( typeName , "GetValue" , type , new Type[0] );
            methodInfo.Invoke( instance , 
null );

            instance 

= Activator.CreateInstance( type , null );
            methodInfo 
= GetMethod( typeName , "Add" , type , new Type[] { typeofint ) , typeofint ) } );
            methodInfo.Invoke( instance , 
new object[] { 1 , 2 } );
        }
static Type GetType ( string typeName )
        {
            Type type 
= null;
            
if ( !typeList.ContainsKey( typeName ) )
            {
                type 
= Type.GetType( typeName );
                typeList.Add( typeName , type );
            }
            
else
                type 
= typeList[typeName];
            
return type;
        }
static MethodInfo GetMethod ( string typeName , string methodName , Type type , params Type[] types )
        {
            MethodInfo methodInfo 
= null;
            
if ( !methodList.ContainsKey( typeName + methodName ) )
            {
                methodInfo 
= type.GetMethod( methodName , types );
                methodList.Add( typeName 
+ methodName , methodInfo );
            }
            
else
                methodInfo 
= methodList[typeName + methodName];
            
return methodInfo;
        }        
    }

其中 Test.TestClass 类和 Test1.Class1 都有一个无参构造函数和一个int类型的参数及2个方法:

int val = 0;
public Class1 ( int val )
{
     
this.val = val;
}
public string GetValue ()
{
     
return string.Format( "the value is {0}" , val );
}
public int Add ( int a , int b )
{
     
return a + b;
}

为了测试,上面2个类里都加了几个方法(只是定义,并没有具体的实现代码).
测试了下不进行任何处理,时间都在130左右波动,对Type进行存储,时间在32左右,基本上没什么波幅,而对Type和MethodInfo进行存储时间维持在31.
看来这样处理下对性能的改善还是起到了作用.