python program running process use time remaining time display function of the implementation code

  • 2021-07-13 05:48:25
  • OfStack

There are many programs that run for a long time. If you don't output the running process, it will be difficult to judge the running time of the program. The following program will output the running process, elapsed time and remaining time according to the format shown in the above figure.


def time_change(time_init):  # Defines a function that converts seconds to time-division-second format 
  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):
      Exactly 
      Exactly 
      Exactly 
    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

Summarize


Related articles: