好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Python实现输出程序执行进度百分比

这篇文章主要介绍了Python实现 输出程序执行进度百分比的方法,涉及Python数值运算与系统 输出相关操作技巧,需要的朋友可以参考下

#-*-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(); 
#-*-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(); 

从求运行百分比的100000次的浮点运算改为100000次的条件运算,同时仅要刷新屏幕1000次,程序的运行耗时将大大减少。

同时,这里值得注意的是,Eclipse中的Pydev中的控制台,对于\r依然是处理成换行符,使得 输出变成如下的样子,这里没有办法了!

以上就是Python实现 输出程序执行进度百分比的详细内容,更多请关注Gxl网其它相关文章!

查看更多关于Python实现输出程序执行进度百分比的详细内容...

  阅读:36次