473,406 Members | 2,633 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Handy short cut for formatting elapsed time in floating point seconds

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

Apr 22 '07 #1
3 4453
On Apr 22, 11:09 am, Paul McGuire <p...@austin.rr.comwrote:
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.

Apr 22 '07 #2
On Apr 21, 9:09 pm, Paul McGuire <p...@austin.rr.comwrote:
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

Apr 22 '07 #3
On Sat, 21 Apr 2007 18:09:01 -0700, Paul McGuire wrote:
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.
--
Steven.

Apr 22 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: NotGiven | last post by:
Below is a good elapsed time function I found. However, I'd like to return total seconds instead of broken down into days, hours, minutes & seconds. In other words, I want "125" instead of "2...
6
by: pallabs | last post by:
I'm new to asp.net and is having problems doing the following: 1. The tab sequence in a asp form is not working properly. The page is a login form and tabindex is set to 0 for username, 1 for...
2
by: DWalker | last post by:
In Visual Studio (Visual Basic) .NET 2002, I noticed that this: Dim Elapsed as DateTime = Now - Now gives a "compile time" error (error in the IDE), saying that the '-' operator is not...
20
by: Jean Johnson | last post by:
Hello - I have a start and end time that is written using the following: time.strftime("%b %d %Y %H:%M:%S") How do I calculate the elapsed time? JJ
40
by: Spiros Bousbouras | last post by:
Do you have an example of an implementation where sizeof(short int) does not divide sizeof(int) or sizeof(int) does not divide sizeof(long int) or sizeof(long int) does not divide sizeof(long long...
1
by: Bill | last post by:
I have a large number of records that have an elapsed time in seconds for each one that I was to have a total time spent. I can sum them up with query easily enough but I need to be able to display...
17
by: Andrew Chalk | last post by:
Network time uses 1900 as its base. The C time library uses 1970. I want to convert a network time value to a C library time. I.e. I have to subtract the number of seconds from 1900 to 1970. Is...
6
by: Jeremy Sanders | last post by:
Hi - I need to add support to a program for dates and times. The built-in Python library seems to be okay for many purposes, but what I would like would be Unix epoch style times (seconds relative...
7
by: wanwan | last post by:
Hi, I'm looking for a better way to do a date/time elapsed event rather than a timer elapsed event. Currently, I wrote my program to use a timer elapsed event set to 1 second, and it checks...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.