在.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(); } } }
下面请看测试结果。
收藏文章
精彩图集
精彩文章
- 暂无记录。
热门标签
PASSWORD
检测移动设备
删除换行符
bind函数
SET类型字段
批量日
常与
电话录音
全局热键
保存到本地
按键精灵
jquery入门
延时启动
24天
itoa
PHP_intl.dll
gps经纬度
fdisk
tmpfs
输入方式
拼音首字母
each()
exec
默认
strtotime()
bmp图片
是否删除
清爽
读写文本文件
PHP pdo 事务
rc.d
常用技巧
异步
doen’t
while
Connect
删除过期文件
Java数组操作
unlink
上移
PHP代码优化
整数
企业版
mysqlimport
查询时区
window.close
pager控件
CDATA
获取id
php php图片
响应式导航条
vbs,组件
必备
SelectIndex
php php在
TableView
MMM安装指南
scrollTop
排序规则
ob_get_conte
赞助商链接
@CopyRight 2002-2008, 1SOHU.COM, Inc. All Rights Reserved QQ:1010969229