python: display elapsed time on shell

2019-07-23 09:14发布

问题:

When I run my Python script, there is some function that takes up to a few minutes to complete, so I want to display on the shell some kind of timer that informs the user on the time elapsed.

Is there any such thing already ready to use in Python?

回答1:

One simplistic way is to include a clock in your sys.ps1 prompt (the thing that normally defines the >>> prompt)

From the documentation for sys.ps1:

If a non-string object is assigned to either variable, its str() is re-evaluated each time the interpreter prepares to read a new interactive command; this can be used to implement a dynamic prompt.

In ~/.local/usercustomize.py (or more accurately, in whatever folder python -c 'import site; print site.USER_BASE' displays), you can add:

import sys
import datetime


class ClockPS1(object):
  def __repr__(self):
    now = datetime.datetime.now()
    return str(now.strftime("%H:%M:%S >>> "))


sys.ps1 = ClockPS1()

Then your prompt will look like this:

16:26:24 >>> import time
16:26:27 >>> time.sleep(10)
16:26:40 >>> 

It's not perfect, as the last time will be when the prompt appeared, not when the line was executed, but it might be of help. You could easily make this display the time in seconds between __repr__ invokations, and show that in the prompt.



回答2:

If you are on a Linux or BSD system, try the pv command (http://www.ivarch.com/programs/pv.shtml).

$ python -c 'import time;time.sleep(5)' | pv
   0B 0:00:05 [   0B/s ] [<=>                   ]

It will give you a timer and depending on how you code the output of your app, some other stats as well.



回答3:

The simplest way to do it would be to calculate the elapsed time in the function that takes a few minutes to complete and simply print that time to the shell. However depending on your function this probably is not the best solution.

The second way to do it would to use multi-threading. So have the function that takes awhile run in a thread, while your program then sits in a loop and prints out the elapsed time every so often and looks for the thread to be completed.

Something like:

import threading
import time
arg1=0
arg2=1
etc=2

# your function that takes a while.
# Note: If your function returns something or if you want to pass variables in/out,
# you have to use Queues
def yourFunction(arg1,arg2,etc):
    time.sleep(10) #your code would replace this

# Setup the thread
processthread=threading.Thread(target=yourFunction,args=(arg1,arg1,etc)) #set the target function and any arguments to pass
processthread.daemon=True
processthread.start() # start the thread

#loop to check thread and display elapsed time
while processthread.isAlive():
    print time.clock()
    time.sleep(1) # you probably want to only print every so often (i.e. every second)

print 'Done'

You can then get fancier by overwriting the time in the shell or even better, using a gui to display a progress bar!



回答4:

Are you talking about measuring the time it takes a function to complete and then print the HH:MM:SS.MS?

You can do:

import datetime, time

time_begin = datetime.datetime.fromtimestamp(time.time())
# call your function here
time_end = datetime.datetime.fromtimestamp(time.time())

print("Time elapsed: ", str(time_end - time_begin))


回答5:

you can use datetime

for example,

import datetime
import time

class Profiler(object):
    def __init__(self):
        self.start = 0
        self.duration = 0

    def start(self):
        self.start = datetime.datetime.now()

    def end(self):
        self.duration = datetime.datetime.now() - self.start

class SomeClass(Profiler):
    def __init__(self):
        Profiler.__init__(self)

    def do_someting(self):
        self.start()
        time.sleep(10)
        self.end()

if __name__ == "__main__":
    foo = SomeClass()
    foo.do_something()
    print 'Time :: ', foo.duration