码迷,mamicode.com
首页 > 其他好文 > 详细

使用表达式树和反射来访问对象属性的性能比较

时间:2014-12-23 22:31:53      阅读:173      评论:0      收藏:0      [点我收藏+]

标签:

今天在工作上遇到这么个需求:需要获取对象上所有属性的值,但并事先并不知道对象的类型。 我的第一反应就是使用反射,但是这个操作会进行多次,大量的反射肯定会有性能影响。虽然对我这个项目无关紧要,但我还是选择了另外一种解决方案:构建表达式树,再生成委托,然后将委托缓存在字典里。代码如下:

首先构建表达式树(类似这种形式:‘(a) => a.xx‘),并生成委托:

private static Delegate BuildDynamicGetPropertyValueDelegate(PropertyInfo property)
{
    var instanceExpression = Expression.Parameter(property.ReflectedType, "instance");
    var memberExpression = Expression.Property(instanceExpression, property);

    var lambdaExpression = Expression.Lambda(memberExpression, instanceExpression);
    return lambdaExpression.Compile();
}

接着,当需要获取属性的值时,先在字典里查看是否有已经生成好的委托,有的话取出委托执行获取属性值。没有则构建表达式树生成委托,并放入字典中:

private static Dictionary<PropertyInfo, Delegate> delegateCache = new Dictionary<PropertyInfo, Delegate>();

public static object GetPropertyValueUseExpression<TObject>(TObject obj, PropertyInfo property)
{         
    if (delegateCache.ContainsKey(property))
    {
        var func = (Func<TObject, object>)delegateCache[property];
        return func(obj);
    }

    var getValueDelegate = BuildDynamicGetPropertyValueDelegate(property);
    delegateCache[property] = getValueDelegate;
    return ((Func<TObject, object>)getValueDelegate)(obj);
}

就这么简单,完成之后,我想测试一下表达式树版本和反射版本的性能差距如何,于是我又简单实现反射版本作为测试对比:

public static object GetPropertyValueUseReflection<TObject>(TObject obj, PropertyInfo propertyInfo)
{
    return propertyInfo.GetValue(obj);
}

接下来是两者的测试代码:

class Car 
{
    public string Make { get; set; }
    public string Model { get; set; }
    public int Capacity { get; set; }
}

.....

int repeatTimes = 10000;
PropertyInfo property = typeof(Car).GetProperty("Make");
Car car = new Car();

Stopwatch stopwatch = Stopwatch.StartNew();
for (int i = 0; i < repeatTimes; i++)
{
    GetPropertyValueUseExpression(car, property);
}
stopwatch.Stop();
Console.WriteLine("Repeated {0}, Cache in Dictionary expression used time: {1} ms", repeatTimes, stopwatch.ElapsedTicks);

stopwatch.Reset();
stopwatch.Start();
for (int i = 0; i < repeatTimes; i++)
{
    GetPropertyValueUseReflection(car, property);
}
stopwatch.Stop();
Console.WriteLine("Repeated {0}, reflection used time: {1} ms", repeatTimes, stopwatch.ElapsedTicks);

在我的预想之中是这样的:表达式树版本在调用次数很少的情况下会慢于反射版本,随着次数增多,表达式树版本的优势会越来越明显。

但是测试结果却出乎我的意料!!!

技术分享

技术分享

技术分享

在调用次数为十万、百万、千万次的情况下,两者所用的时间差不多,而且反射版本居然还要快一些。这可让我郁闷不已。

郁闷之后,我就在想是不是因为字典的原因导致两者性能差不多,就添加了以下测试代码:

stopwatch.Reset();
stopwatch.Start();
var func = (Func<Car, object>)BuildDynamicGetPropertyValueDelegate(property);
for (int i = 0; i < repeatTimes; i++)
{
    func(car);
}
stopwatch.Stop();
Console.WriteLine("Repeated {0}, Immediate call expression used time: {1} ticks", repeatTimes, stopwatch.ElapsedTicks);

这部分测试代码,在构建表达式树生成委托之后,直接调用,去除了字典的影响。测试结果如下:

技术分享

果不其然,去除字典之后速度快了10倍。

看来在我这种情况下使用字典缓存委托的效果并不是太好。不知道是否有更好的方法来缓存委托。

最后,如果我的代码有错误或者测试方法不对,欢迎大家指出

 

使用表达式树和反射来访问对象属性的性能比较

标签:

原文地址:http://www.cnblogs.com/hao-dotnet/p/4181283.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!