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

yipeiwu_com5年前Python基础

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

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程序运行进程、使用时间、剩余时间显示功能的实现代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对【听图阁-专注于Python设计】网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

相关文章

浅谈pytorch grad_fn以及权重梯度不更新的问题

前提:我训练的是二分类网络,使用语言为pytorch Varibale包含三个属性: data:存储了Tensor,是本体的数据 grad:保存了data的梯度,本事是个Variable...

TensorFlow dataset.shuffle、batch、repeat的使用详解

直接看代码例子,有详细注释!! import tensorflow as tf import numpy as np d = np.arange(0,60).reshape([6...

基于ID3决策树算法的实现(Python版)

基于ID3决策树算法的实现(Python版)

实例如下: # -*- coding:utf-8 -*- from numpy import * import numpy as np import pandas as pd fr...

Python数据分析:手把手教你用Pandas生成可视化图表的教程

Python数据分析:手把手教你用Pandas生成可视化图表的教程

大家都知道,Matplotlib 是众多 Python 可视化包的鼻祖,也是Python最常用的标准可视化库,其功能非常强大,同时也非常复杂,想要搞明白并非易事。但自从Python进入3...

Python 文件重命名工具代码

复制代码 代码如下:#Filename:brn.py #Description: batch replace certain words in file names #Use to ba...