在.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...与不使用,看下性能如何:
测试时用到的类
Code [http://www.xueit.com]
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; } } }
测试页的代码
Code [http://www.xueit.com]
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(); } } }
下面请看测试结果。
精彩图集
精彩文章
热门标签
糗百图片
人数
unref()
事件管理
安装方
子程序
信号
导致定时器失
扩展插件
灾难性故障
strpos函数
循环集合
检测lvs
CDATA的作用说明
BMP
字
ORM
三元运算符
modu
直接
简单的
伪装
soap消
ff
id未定义
tar命令错误
coroutine
修改文件名
数目
磁盘管理
安装配置
竖排
对象指针
受访
提交事件
窗口传参
php php写
Socks5
控制符
js基础教程
opendir(
phpnow
排序去重
内存溢出
name
php PHP自
易理解
对象初始值
jsp文件
WPF
等待处理
文件上传控件
枚举窗口句柄
consistent
javascript知识
hashCode
时分秒
口语化
异步执行
动态图片
赞助商链接
@CopyRight 2002-2008, 1SOHU.COM, Inc. All Rights Reserved QQ:1010969229

