Python implementation Progress percentage for output program

Source: Internet
Author: User
This article mainly describes the Python implementation of the output program execution percentage of the method, involving Python numerical operations and system output related operation skills, the need for friends can refer to the following

The example in this article describes how Python implements the percentage of output program execution progress. Share to everyone for your reference, as follows:

For some large Python programs, we need to output their percentages on the command line, which is more friendly, so as not to be misunderstood by the program into the dead loop, the form of suspended animation.
The key is to take advantage of the output of the non-newline output character \r,\r, which will overwrite the contents of this line directly.

such as the following program, is an I from 0 to add 100,000 of the process, even for the current high-performance CPU also takes a few seconds, we want to output its execution time percentage, you can introduce SYS this package, Use to sys.stdout.write output, avoid the original eco-print to bring the impact of the overall situation. The decimal place to control the percentage is also 4. The percentage of program execution is exactly the current value of I divided by the total of the value 100,000.


#-*-coding:utf-8-*-import sys;total=100000for 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 ();

The results of the program run as follows:

However, here I per self-increment to require the current percentage of the operation, the original 100,000 times the floating-point operation in vain to increase to 200,000 times, and to refresh the screen 100,000 times, very unreasonable, so for the program, you can do the following improvements, the running percentage is reserved only 2 decimal places, At the same time I do a percent output per cumulative 100, after the program is modified as follows:


#-*-coding:utf-8-*-import sys;total=100000for 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 ();

The operation time of the program is greatly reduced by changing the 100,000-time floating-point operation of the running percentage to the 100,000-time conditional operation while only refreshing the screen 1000 times.

At the same time, it is worth noting that the Pydev console in Eclipse, for \ R is still processed into a newline character, so that the output becomes as follows, there is no way!

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.