In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
Python optimization how to calculate the execution time of each statement, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can gain something.
Programs written in Python do have worse performance than other languages because Python sacrifices a certain amount of running efficiency in order to maximize development efficiency. Development efficiency and operational efficiency are often the relationship between a fish and a bear's paw.
However, there are many reasons for the poor performance of the program, we can not throw all the pot on Python, we should first find the reason from our own code, and the quickest way to find the reason is to calculate the execution time of the statement or function we wrote. At this time, many people will choose to print the execution time of the statement in the following form:
This is an inefficient approach, and if you have tens of thousands of sentences to test, it's like looking for a needle in a haystack to find a bottleneck. Fortunately, thanks to the powerful community capabilities of Python, we have a lot of efficiency modules available, and today we're going to introduce line_profiler, which calculates the time spent on each statement in the function.
We will use the code from the face recognition in the last movie to explain: Python recognizes faces in the movie, but please note that the function in this push has passed a few parameters. Please click on the original text to read the correct parameters.
1. Prepare for
The Python environment is of course a must, and if you don't already have Python installed, you can read this article: hyperdetailed Python installation Guide.
Open cmd/terminal and enter the following command to install line_profile:
Pip install line_profiler
If the windows machine has an error such as Microsoft Visual C++ 14.0 is required, please go to Microsoft's official website and download the vs2015 check "Public tools for visual computers 2015" to install.
If: ModuleNotFoundError: No module named 'skbuild' occurs, enter the following command to install scikit-build:
Pip install scikit-build
If you can't install it, you can download anaconda and enter the following command to install it:
Conda install-c anaconda line_profiler
two。 Use
The way to use it is very simple. For example, when we read the code for human face, the main function looks like this:
What we want to measure is the execution time of all the statements in the read_pic_save_face function, just call line_profiler like this:
In this way, you can get the execution time report of all statements of the function. Of course, it also has many other calling methods, as shown in the line_profiler documentation:
Https://github.com/rkern/line_profiler
3. Read the report
The line_profiler report consists of several parts:
Line: which line is the statement on
Hits: the number of times the row was executed
Time: the total time that the statement was run
Per Hit: the average time it takes to run this statement once
% Time: the proportion of the statement to the total time
As you can see, our code takes the longest time in face_cascade.detectMultiScale, which is a problem of opencv classifier execution efficiency. Knowing that it is a problem of efficiency here, optimization has a goal.
In this part of the optimization, we can start with the hardware and let OpenCV run the algorithm on GPU, which will far exceed the performance of running on CPU. The second is to use multithreaded computing (I haven't tried, I'm not sure if it's useful, maybe I can try it next time).
Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.