.NET比较表达式树对性能影响(3)
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());
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来比较呢,哈哈。
说明:这种表达式树只生成一次,即可完成整个实体集的应用,起到了非常好的复用作用。
表达式树的优势:
表达式树拥有语义清晰,强类型等优势,表达式树的计算对于性能的影响会越来越大,由于减少了编译操作和反射操作的次数,计算所需开销大大降低。
- 上一篇:C#农历计算查询(公历与农历之间转换)
- 下一篇:.net剪切图片源码
精彩图集
精彩文章