python程序运行进程、使用时间、剩余时间显示功能的实现代码-创新互联

有很多程序运行时间比较长,如果不将运行过程输出将很难判断程序运行的时间。下边这段程序将按照上图所示的格式输出程序运行进程、已用时间、剩余时间。

成都创新互联公司-专业网站定制、快速模板网站建设、高性价比石鼓网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式石鼓网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖石鼓地区。费用合理售后完善,十余年实体公司更值得信赖。
def time_change(time_init):  #定义将秒转换为时分秒格式的函数
  time_list = []
  if time_init/3600 > 1:
    time_h = int(time_init/3600)
    time_m = int((time_init-time_h*3600) / 60)
    time_s = int(time_init - time_h * 3600 - time_m * 60)
    time_list.append(str(time_h))
    time_list.append('h ')
    time_list.append(str(time_m))
    time_list.append('m ')
  elif time_init/60 > 1:
    time_m = int(time_init/60)
    time_s = int(time_init - time_m * 60)
    time_list.append(str(time_m))
    time_list.append('m ')
  else:
    time_s = int(time_init)
  time_list.append(str(time_s))
  time_list.append('s')
  time_str = ''.join(time_list)
  return time_str
if __name__=="__main__":
  process = .0
  start = time.time()
  for i in range(total_num):
     ···
     ···
     ···
    if process < (i*1.0/total_num):
      if process != 0:
        end = time.time()
        use_time = end-start
        all_time = use_time / process
        res_time = all_time - use_time
        str_ues_time = time_change(use_time)
        str_res_time = time_change(res_time)
        print("Percentage of progress:%.0f%%  Used time:%s  Rest time:%s "%(process*100,str_ues_time,str_res_time))
      process = process + 0.01

本文题目:python程序运行进程、使用时间、剩余时间显示功能的实现代码-创新互联
网页链接:http://pwwzsj.com/article/pssho.html