在.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(); } } }
下面请看测试结果。
收藏文章
精彩图集
精彩文章
- 暂无记录。
热门标签
两数相加
optionParser
十进制数
问
左端
Dom
自定义排序
vbsTree
ip限制
父div
power
等比缩略图
提交按钮
链接库
无法复制
php php无限分类
多文件上传
多主复制
归档模式
自动升级
数据创建
进程占用
实时推送
OpenGL
myisam
SqlServer201
公钥
解
层次关系
chroot
机器
python模块
经典
密码强度
绘制文字
jpgraph类库
http
连接mysql数据库
级联更新
dns放大攻击
hql语句
r2
效果
bing每日壁纸
开机
md5_file
图形 缩略图类
__new__
后台处
filesor
divcss
发送短信
回文数
resolv.conf
img标记
办法
dbms_random
取数据
人人网登录
启动故障
赞助商链接
@CopyRight 2002-2008, 1SOHU.COM, Inc. All Rights Reserved QQ:1010969229