ASP.NET性能:哪个更快? [英] ASP.NET performance: which is faster?

查看:146
本文介绍了ASP.NET性能:哪个更快?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里有选择。两个意见:



一个是读一个XML文件,大约一页长,两次,并尝试找出是否可以找到某个属性值并分配到一个字符串或不。首先是找出属性是否存在,而不是null。第二次读取并分配值。

  If([xmlAttribute]!= null){
string = xmlAttribute ;
}

两个只是读取相同的XML文件一次,并尝试直接分配值没有尝试找到它第一。如果失败,它将抛出一个异常,catch块将字符串分配给null。

  try {
string = [xmlAttribute];
} catch(Exception ex){
string = null;
}

哪种方式更快?还是更好的主意?
谢谢。

解决方案

创建异常有很多开销 - 有关该方法的详细信息,堆栈跟踪,并且潜在的错误是非常耗时的收集。使用异常作为预期程序逻辑的一部分是差的编码。



寻找一种检查数据的方法,尽可能不抛出异常。


I have a choice here. Two opinions:

One is to read an XML file, which is about one page long, twice and try to find out if I can find a certain attribute value and assign it to a string or not. First time is to find out if the attribute exists and not null. Second time to read and assign the value.

If([xmlAttribute]!= null){
  string = xmlAttribute;
}

Two is just read the same XML file once and try to assign the value directly without try to find it first. If failed, it will throw an exception and the catch block will assign the string to null.

try{
  string = [xmlAttribute];
}catch(Exception ex){
  string = null;
}

Which way is faster? Or any better idea? Thanks.

解决方案

There is a lot of overhead to creating an Exception--details about the method, stack trace, and underlying error is very time-consuming to collect. Using Exceptions as part of expected program logic is poor coding.

Look for a way to check the data without throwing the exception wherever possible.

这篇关于ASP.NET性能:哪个更快?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆