Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
189 views
in Technique[技术] by (71.8m points)

c# - ASP.NET performance: which is faster?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...