在.NET(C#)使用Try...Catch...会影响效率吗?测试一下
下面测试下在.NET使用Try...Catch...与不使用,看下性能如何: 测试时用到的类 Code [http://www.xueit.com] using System; using System.Diagnostics; namespace WebApplication3{ public static class Test { public static void
下面测试下在.NET使用Try...Catch...与不使用,看下性能如何:
测试时用到的类

using System; using System.Diagnostics; namespace WebApplication3 { public static class Test { public static void NoTry() { for (int i = 0; i < 10000; i ) { System.Web.HttpContext.Current.Response.Write(i.ToString()); } } public static void HaveTry() { try { for (int i = 0; i < 10000; i ) { System.Web.HttpContext.Current.Response.Write(i.ToString()); } } catch(Exception err) { System.Web.HttpContext.Current.Response.Write(err.ToString()); } } public static long HaveException() { Stopwatch sw = new Stopwatch(); sw.Start(); try { for (int i = 0; i < 10000; i ) { System.Web.HttpContext.Current.Response.Write(i.ToString()); } throw new Exception("Kevin让我异常了"); } catch (Exception err) { System.Web.HttpContext.Current.Response.Write(err.ToString()); } sw.Stop(); return sw.ElapsedMilliseconds; } } }
测试页的代码

using System; using System.Diagnostics; namespace WebApplication3 { public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { Stopwatch sw = new Stopwatch(); sw.Start(); Test.NoTry(); sw.Stop(); lblMessage.Text = "测量实例得出的总运行时间(毫秒为单位):" sw.ElapsedMilliseconds; } protected void Button2_Click(object sender, EventArgs e) { Stopwatch sw = new Stopwatch(); sw.Start(); Test.NoTry(); sw.Stop(); lblMessage.Text = "测量实例得出的总运行时间(毫秒为单位):" sw.ElapsedMilliseconds; } protected void Button3_Click(object sender, EventArgs e) { lblMessage.Text = Test.HaveException().ToString(); } } }
下面请看测试结果。
精彩图集
精彩文章
热门标签
第三天
inner
SQLObject
jqeury
扩大分辨率
linux配置防火墙
设置宽度
print_r()
SendKeys参考文档
201
Dictionary
定长内存池
继续
mbstring扩展
gravity
from表单
PHP PHP随
写日志文件
line-height
通话记录
格式判断
函数参数
调用c
java验证码
序列号
Guard
磁盘配额
原生事件
应用
可拖动窗口
内存管理
uncheck
半角
记住用户名
gbk字符集
并发
deeplink
据库表结构
经验总结
删除服务
挂载目录
水平居中
AnalogCl
Windows服务器
最小值
table高级应用
python复制文件
函数实例
ViewFlipper
修改字符集
获得焦点
右外联
phpwind
css对联广告
文件类型
MessagePack
c++遍历
backtrak
表达式查
keyCode
赞助商链接
@CopyRight 2002-2008, 1SOHU.COM, Inc. All Rights Reserved QQ:1010969229