python: display elapsed time on shell

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?


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/ (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 =
    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.

If you are on a Linux or BSD system, try the pv command (

$ 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.

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

# 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.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!

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))

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 =

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

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

    def do_someting(self):

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

Need Your Help

find "frozenNode" by property

jackrabbit jcr jcr-sql2

We have our custom node type "ns:Document" with property "ns:id". Is it possible to find node (ns:Document) by property (ns:id) when we increase document version and save new document content with ...

Why won't this regexp work in google spreadsheets?

regex spreadsheet expression

I'm trying to extract from a url using a regexp in google spreadsheets. However the spreadsheet returns #VALUE! with the following error: Invalid regular expression: invalid perl operator: (?&lt;...