"Performance vs stability". Net's try-catch efficiency

Source: Internet
Author: User

Reprint please mark: Shu Xiaolong http://www.cnblogs.com/shuxiaolong/archive/2013/04/10/3012439.html

 

Cause:

In most projects, try-catch is not added. Ask why and answer the question about performance;

However, no matter whether the details are correct or not, the errors are caused by exceptions (program or website Yellow Pages). Developers are tired of following up on these bugs;

So I want to know how much efficiency is wasted by try-catch;

 

Code:

1 datetime time1 = datetime. now; 2 for (INT I = 0; I <10000000; I ++) 3 {4 try {int temp = convert. toint32 ("1234567890");} 5 catch (exception exp) {} 6} 7 datetime time2 = datetime. now; 8 for (INT I = 0; I <10000000; I ++) 9 {10 int temp = convert. toint32 ("1234567890"); 11} 12 datetime time3 = datetime. now; 13 14 console. writeline ("Time Difference (MS): \ t has try \ t {0}", (time2-time1 ). totalmilliseconds); 15 console. writeline ("Time Difference (MS): \ t no try \ t {0}", (time3-time2 ). totalmilliseconds); 16 console. writeline ();

 

Result: (run 10 times)

Note: 7th, 10th times: the number of try-catch instances exceeds that of try-catch instances.

10 million try-catch operations only take 1.6 seconds.

 

If the error occurs:

1 datetime time1 = datetime. now; 2 for (INT I = 0; I <10000000; I ++) 3 {4 try {int temp = convert. toint32 ("ABC");} 5 catch (exception exp) {} 6} 7 datetime time2 = datetime. now; 8 console. writeline ("Time Difference (MS): \ t has try \ t {0}", (time2-time1 ). totalmilliseconds ));

Catch performance instantly loses 200 times;

 

Conclusion:

Try is not a waste of efficiency. Catch is a waste of efficiency;

Catch will waste your efficiency, and will crash without catch;

Change the question to "How do you choose to waste efficiency during errors and program crashes during errors ?"

 

 

Case:

In the previous company, there were 6 servers on one website, which were visited by 40 million people every day;

Development Department Code requirements: Your modules do not affect others' modules, so in order to avoid accidents, the code is common: a function is try from the beginning, catch at the end of the function to write exception logs (try-catch is like a part of the function body );

Result: The website is stable and has high performance.

 

 

My thoughts:

The real performance is not that you don't need try-catch; you don't need LINQ; you don't need EF;

Projects that have really participated in, especially projects that deliver others (excluding products and main websites) are not efficient. They are often:

Incorrect algorithms (such as memory paging );

The dog skin plaster that will not cure the symptoms in the later stage (as long as the bug keeps him from reporting an error );

Code that can not be executed is executed in the architecture;

 

Real stability is not necessarily a try-catch anytime, anywhere;

Carefully write the code of each function (try-catch can be added when exceptions may occur) or (even if exceptions occur) or (certainly no exception Code );

Do not trust the returned values of calls from other methods-do not think that other methods will certainly return the value you need;

For all network communications, try-catch must be added to distributed systems;

 

 

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.