Example analysis of the impact of Python exceptions on code running performance

Python's exception handling ability is very powerful, but bad use will also have a negative impact. I also like to use exceptions in the process of writing programs. Although it would be better to code in a defensive way, it will play a lazy role to hand it over to exception handling. I occasionally think about how much impact exception handling will have on performance, so I tried to test it today.

Exceptions are allowed, but care must be taken.

An exception is a way to jump out of the normal control flow of a code block to handle errors or other exception conditions.

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

It may lead to confusing control flow. It is easy to miss error conditions when calling the library.

Exceptions must comply with certain conditions:

Trigger exceptions like this: raise myexception ("error message") or raise myexception. Do not use the form of two parameters (raise myexception, "error message") or outdated string exception (raise "error message"). The 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".

Never use the exception: statement to catch all exceptions, exception or standarderror, unless you intend to re trigger the exception, or you are already at the outermost layer of the current thread (remember to print an error message). Python is very tolerant of exceptions, Exception: really catch any errors, including Python syntax errors. It's easy to hide real bugs with exception.

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

Use the finally clause to execute the code that should be executed regardless of whether there are exceptions in the try block. This is often useful for cleaning up resources, such as closing files. When catching exceptions, use as instead of commas. For example

Take a relatively simple and intuitive control experiment.

First, define a decorator to calculate the execution time of each function:

Then use the decorator to decorate the tested function.

Define another one called do_ The function of something, which does one thing, assigns 1 to variable a. In each test function, this function will be called 1000000 times.

do_ something:

I designed different test groups according to the situation:

Test group 1 (direct execution of time-consuming operations):

Test group 2 (time-consuming operations are executed in try without throwing errors):

Test group 3 (try is put into time-consuming operation, and no error is thrown for each operation of try):

Test group 4 (try is put into time-consuming operation, try every operation and handle exceptions (catch specific exceptions thrown)):

Test group 5 (try is put into time-consuming operation, try every operation and handle exceptions (catch all exceptions try... Except baseexception)):

Test group 6 (try is put into time-consuming operation, try every operation and handle exceptions (catch all exceptions without any exception type)):

Test group 7 (time-consuming operations are placed in except):

Test group 8 (defensive coding):

results of enforcement

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>