Python implements output program execution progress percentage

巴扎黑
Release: 2017-09-18 10:18:35
Original
2099 people have browsed it

This article mainly introduces the method of Python to realize the output program execution progress percentage, involving Python numerical operations and system output related operation skills. Friends in need can refer to the following

The example of this article describes the implementation of the output program in Python Method to execute progress percentage. Share it with everyone for your reference, the details are as follows:

For some large Python programs, we need to output the percentage on the command line to appear more friendly, so as to avoid being misunderstood that the program falls into an infinite loop or suspended animation form.
The key is to use the output character \r without line breaks. The output of \r will directly overwrite the content of this line.

For example, the following program is a process in which i is increased from 0 to 100,000. Even for current high-performance CPUs, it takes several seconds. If we want to output the percentage of execution, we can After introducing the sys package, the sys.stdout.write output is used to avoid the \n inherent in the original print from affecting the overall situation. At the same time, the decimal place of the percentage should be controlled to 4. The percentage of program execution is exactly the current value of i divided by total, which has a value of one hundred thousand.


#-*-coding:utf-8-*-
import sys;
total=100000
for i in range(0,total):
  percent=float(i)*100/float(total)
  sys.stdout.write("%.4f"%percent);
  sys.stdout.write("%\r");
  sys.stdout.flush();
sys.stdout.write("100%!finish!\r");
sys.stdout.flush();
Copy after login

The program running result is as follows:

However, every time i is incremented here, the current running result is required. Percentage, the original 100,000 floating-point operations are increased to 200,000 times, and the screen needs to be refreshed 100,000 times at the same time. It is very unreasonable. Therefore, the following improvements can be made to the program. The running percentage only retains 2 decimal places. At the same time, i The percentage output is performed every time 100 is accumulated. The program is modified as follows:


#-*-coding:utf-8-*-
import sys;
total=100000
for i in range(0,total):
  if i%100==0:
    percent=float(i)*100/float(total)
    sys.stdout.write("%.2f"%percent);
    sys.stdout.write("%\r");
    sys.stdout.flush();
sys.stdout.write("100%!finish!\r");
sys.stdout.flush();
Copy after login

The 100,000-time floating-point operation for calculating the running percentage is changed to 100,000-time conditional operation. At the same time, only To refresh the screen 1000 times, the running time of the program will be greatly reduced.

At the same time, it is worth noting here that the console in Pydev in Eclipse still processes \r into a newline character, making the output look like the following. There is no way here!

The above is the detailed content of Python implements output program execution progress percentage. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!