Current location - Health Preservation Learning Network - Healthy weight loss - Example analysis method of Python exception's influence on code running performance
Example analysis method of Python exception's influence on code running performance
order

Python's exception handling ability is very powerful, but it will also bring negative effects if it is not used well. I also like to use exceptions when writing programs. Although it will be better to code in a defensive way, handing it over to exception handling will play a lazy role. I occasionally think about how much exception handling will affect performance, so I tried to test it today.

Python Exception (Google Open Source Style Guide)

Tip:

Exceptions are allowed, but care must be taken.

Definition:

Exception is a way to jump out of the normal control flow of a code block to handle errors or other abnormal situations.

Advantages:

The control flow of normal operation code will not be mixed with error handling code. It also allows the control flow to skip multiple frames when a certain condition occurs. For example, jump out of n nested functions in one step without continuing to execute the wrong code.

Disadvantages:

This may lead to chaotic control flow. It is easy to miss the wrong situation when calling the library.

Conclusion:

Exceptions must meet certain conditions:

Trigger exceptions like this: throw MyException (error message) or throw MyException. Do not use the form of two parameters (throwing MyException, error message) or expired string exception (throwing error message).

A module or package should define its own domain-specific exception base class, which should inherit from the built-in exception class. The exception base class of the module should be called "Error".

1

2

classes

Error (exception):

get through

Never use the except: statement to catch all exceptions, and don't catch exceptions or StandardError unless you plan to trigger exceptions again, or you are already at the outermost layer of the current thread (remember to print an error message). Python is very tolerant of exceptions, except that it really catches any errors, including Python syntax errors. Use except: It's easy to hide real bugs.

Try to reduce the amount of code in the try/except block. The larger the try block, the easier it is to trigger unexpected exceptions. In this case, the try/except block will hide the real error.

Use the finally clause to execute the code that should be executed, regardless of whether there is an exception in the try block. This is usually useful for cleaning up resources, such as closing files.

When catching exceptions, use as instead of commas. take for example

1234

attempt

rise

mistake

Except for ...

Error is error:

get through

Design experimental mode

Take a relatively simple and intuitive control experiment.

First, define a decorator to calculate the time required for each function to execute:

123456789 10

Excellent

Timer (function):

Imported?

time

Excellent

Packaging (*

Parameters,

*

*

kwargs):

start time

=

time.time()