龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > web编程 > asp.net编程 >

.NET比较表达式树对性能影响(3)

时间:2009-12-21 11:47来源:未知 作者:admin 点击:
分享到:
4:性能比较: List Book list = this .GetData(); SetAllPropertyMethod Book s = new SetAllPropertyMethod Book (); Stopwatchsw = new Stopwatch(); sw.Start(); list = s.SetAllProperty(list, " sTitle "
    
       4:性能比较:
            

List<Book> list = this.GetData();
            SetAllPropertyMethod
<Book> s = new SetAllPropertyMethod<Book>();         
            Stopwatch sw 
= new Stopwatch();
            sw.Start();
            list 
= s.SetAllProperty(list, "sTitle""1");
            sw.Stop();
            
double time1 = sw.ElapsedMilliseconds;

            Response.Write(
"反射方式用时:"+time1 .ToString ());

            sw 
= new Stopwatch();
            sw.Start();
            list 
= s.ExpressionTree<Book>(list, "sTitle""1");
            sw.Stop();
            time1 
= sw.ElapsedMilliseconds;
            Response.Write(
"表达式树用时:" + time1.ToString());

      
       5:输出结果如下:反射方式用时:3198表达式树用时:38 ,可能看出接近100倍的性能优势。

            说话这个Stopwatch原来我也没有用过,汗,编码快四年了,居然这种好东西没用过。是不是有些朋友也和我一要只会利用TimeSpan来比较呢,哈哈。
            说明:这种表达式树只生成一次,即可完成整个实体集的应用,起到了非常好的复用作用。

       表达式树的优势:
           表达式树拥有语义清晰,强类型等优势,表达式树的计算对于性能的影响会越来越大,由于减少了编译操作和反射操作的次数,计算所需开销大大降低。


精彩图集

赞助商链接