Handy short cut for formatting elapsed time in floating point seconds

P

Paul McGuire

I am doing some simple timing of some elements of Python scripts, and
the simplest is to just call time.time() before and after key elements
of the script:

t1 = time.time()

# do lengthy operation

t2 = time.time()
print "That took %f seconds" % (t2-t1)

Unfortunately, this gives very ugly timing output, as just a floating
point number of seconds. After several iterations of writing a
formatter (strftime is not straightforward to use - it omits
milliseconds for one thing), I came up with this:

def secondsToStr(t):
rediv = lambda ll,b : list(divmod(ll[0],b)) + ll[1:]
return "%d:%02d:%02d.%03d" % tuple(reduce(rediv,[[t*1000,],
1000,60,60]))

Now I can write:

print "That took", secondsToStr(t2-t1),"seconds"

and get nicely-formatted 0:00:12.345 style output.

(I also posted this to the Python Cookbook.)

-- Paul
 
J

John Machin

I am doing some simple timing of some elements of Python scripts, and
the simplest is to just call time.time() before and after key elements
of the script:

t1 = time.time()

# do lengthy operation

t2 = time.time()
print "That took %f seconds" % (t2-t1)

Why would you do %f and not %.3f ?
Unfortunately, this gives very ugly timing output, as just a floating
point number of seconds. After several iterations of writing a
formatter (strftime is not straightforward to use - it omits
milliseconds for one thing), I came up with this:

def secondsToStr(t):
rediv = lambda ll,b : list(divmod(ll[0],b)) + ll[1:]
return "%d:%02d:%02d.%03d" % tuple(reduce(rediv,[[t*1000,],
1000,60,60]))

Now I can write:

print "That took", secondsToStr(t2-t1),"seconds"

and get nicely-formatted 0:00:12.345 style output.

and it it runs for (say) 83+ seconds you will get
"That took 0:01:23.456 seconds" which is ludicrous.
And if you want to calculate a percentage speed-up later you will have
to convert back to seconds.
(I also posted this to the Python Cookbook.)

-- Paul

IMHO if your job is short,
print "That took %.3f seconds" % (t2 - t1)
is adequate, and your leading 0:00: is useless.
If it runs for minutes or hours, then minutes or hours to (say) 4
significant digits is more than adequate, and printing milliseconds is
useless.
 
G

George Sakkis

I am doing some simple timing of some elements of Python scripts, and
the simplest is to just call time.time() before and after key elements
of the script:

t1 = time.time()

# do lengthy operation

t2 = time.time()
print "That took %f seconds" % (t2-t1)

Unfortunately, this gives very ugly timing output, as just a floating
point number of seconds. After several iterations of writing a
formatter (strftime is not straightforward to use - it omits
milliseconds for one thing), I came up with this:

def secondsToStr(t):
rediv = lambda ll,b : list(divmod(ll[0],b)) + ll[1:]
return "%d:%02d:%02d.%03d" % tuple(reduce(rediv,[[t*1000,],
1000,60,60]))

Now I can write:

print "That took", secondsToStr(t2-t1),"seconds"

and get nicely-formatted 0:00:12.345 style output.

(I also posted this to the Python Cookbook.)

-- Paul

Cute... for obfuscated python contests :) Whenever I needed this, a
simple
"Completed in %d minutes and %.1f seconds" % divmod(end-start, 60)
was more than enough.

George
 
S

Steven D'Aprano

I am doing some simple timing of some elements of Python scripts, and
the simplest is to just call time.time() before and after key elements
of the script:

t1 = time.time()

# do lengthy operation

t2 = time.time()
print "That took %f seconds" % (t2-t1)

I dispute that this is easiest. I think the timeit module is easier, and
more reliable.

You probably should be putting the lengthy operation in a function (you're
going to run the test more than once, right?), so you can do this:


import timeit
timeit.Timer("function()", "from __main__ import function").repeat()


or even:

s = """
# insert your code
# for the length operation
# here
"""
timeit.Timer(s, "").repeat()


Call help(timeit) for more details.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top