反射性能测试
源代码:TestReflection.rar
1、新建一个空的解决方案。
2、向解决方案中添加三个类库项目,分别为IDAL,DAL,BLL。
3、新建一个用于测试的WEB项目。
4、向IDAL中添加IReflection接口:
using System;
using System.Collections.Generic;
using System.Text;
namespace IDAL
{
public interface IReflection
{
void Test();
}
}
5、向DAL中添加ReflectionDA类:
using System;
using System.Collections.Generic;
using System.Text;
using IDAL;
namespace DAL
{
public class ReflectionDA:IReflection
{
public void Test()
{
return;
}
}
}
6、向BLL添加测试类:
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using IDAL;
namespace BLL
{
/// <summary>
/// 测试反射的效率
/// </summary>
/// <remarks>
/// 作者:☆聊ゾ聊☆
/// 时间:2007-03-26
/// </remarks>
public class ReflectionBL
{
/// <summary>
/// 每次反射创建一个实例,然后利用接口调用类的方法
/// </summary>
/// <param name="count">方法调用次数</param>
/// <returns>调用方法所花费的时间</returns>
public TimeSpan TestFromInterface(int count)
{
//反射调用
DateTime startR = DateTime.Now;
for (int i = 0; i < count; i++)
{
Assembly assembly = Assembly.LoadFile(GetDLLPath());
Type type = assembly.GetType("DAL.ReflectionDA");
ConstructorInfo conInfo = type.GetConstructor(new Type[] { });
object obj = conInfo.Invoke(new object[] { });
IReflection reflection = (IReflection)obj;
reflection.Test();
}
return DateTime.Now - startR;
}
/// <summary>
/// 每次反射创建一个实例,然后反射调用类的方法
/// </summary>
/// <param name="count">方法调用次数</param>
/// <returns>调用方法所花费的时间</returns>
public TimeSpan TestFromReflection(int count)
{
//基于接口的反射调用
DateTime startR = DateTime.Now;
for (int i = 0; i < count; i++)
{
Assembly assembly = Assembly.LoadFile(GetDLLPath());
Type type = assembly.GetType("DAL.ReflectionDA");
ConstructorInfo conInfo = type.GetConstructor(new Type[] { });
object obj = conInfo.Invoke(new object[] { });
object rtn;
rtn = type.GetMethod("Test").Invoke(obj, new object[] { });
}
return DateTime.Now - startR;
}
/// <summary>
/// 每次用new 关键字实例化一个类,并调用其方法
/// </summary>
/// <param name="count">方法调用次数</param>
/// <returns>调用方法所花费的时间</returns>
public TimeSpan Test(int count)
{
DateTime startR = DateTime.Now;
for (int i = 0; i < count; i++)
{
DAL.ReflectionDA dal = new DAL.ReflectionDA();
dal.Test();
}
return DateTime.Now - startR;
}
/// <summary>
/// 获取动态链接库所在的路径
/// </summary>
/// <returns>动态链接库所在的路径</returns>
private string GetDLLPath()
{
return @"F:\localhost\TestReflection\DAL\bin\Debug\DAL.dll";
}
/// <summary>
/// 先用反射创建对象,然后再利用接口调用方法
/// </summary>
/// <param name="count">调用方法所花费的时间</param>
/// <returns>调用方法所花费的时间</returns>
public TimeSpan TestFromInterface1(int count)
{
Assembly assembly = Assembly.LoadFile(GetDLLPath());
Type type = assembly.GetType("DAL.ReflectionDA");
ConstructorInfo conInfo = type.GetConstructor(new Type[] { });
object obj = conInfo.Invoke(new object[] { });
//反射调用
DateTime startR = DateTime.Now;
for (int i = 0; i < count; i++)
{
IReflection reflection = (IReflection)obj;
reflection.Test();
}
return DateTime.Now - startR;
}
/// <summary>
/// 先创建对象的实例,再反射调用方法
/// </summary>
/// <param name="count">调用方法所花费的时间</param>
/// <returns>调用方法所花费的时间</returns>
public TimeSpan TestFromReflection1(int count)
{
//基于接口的反射调用
Assembly assembly = Assembly.LoadFile(GetDLLPath());
Type type = assembly.GetType("DAL.ReflectionDA");
ConstructorInfo conInfo = type.GetConstructor(new Type[] { });
object obj = conInfo.Invoke(new object[] { });
DateTime startR = DateTime.Now;
for (int i = 0; i < count; i++)
{
object rtn;
rtn = type.GetMethod("Test").Invoke(obj, new object[] { });
}
return DateTime.Now - startR;
}
}
}
在我本地机器上的测试结果如下:
频繁创建对象实例:
反射方法:00:00:01.9843750
接口调用:00:00:01.9687500
提前创建对象实例:
反射方法:00:00:00.3750000
接口调用:00:00:00
直接调用:00:00:00
机器配置:AMD 3000+ 64位/1G内存/MS WIN2K3