[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Script Running Time

ewanfisher

2/21/2008 6:38:00 PM

Hello,

I am trying to find a way to output how long a script took to run.

Obviously the print would go at the end of the script, so it would be
the time up till that point. I also run a PostgreSQL query inside the
script and would like to separately show how long the query took to
run.

Is this even possible?

Thanks,

Ewan
1 Answer

Tim Chase

2/21/2008 7:20:00 PM

0

> I am trying to find a way to output how long a script took to run.
>
> Obviously the print would go at the end of the script, so it would be
> the time up till that point. I also run a PostgreSQL query inside the
> script and would like to separately show how long the query took to
> run.
>
> Is this even possible?

Of course...depending on the resolution you need, you can do
something like

import datetime
start_script = datetime.datetime.now()
# do stuff
start_postgresql = datetime.datetime.now()
# make your PG call
end_postgresql = datetime.datetime.now()
# do remaining stuff
end_script = datetime.datetime.now()
pg_time_taken = end_postgresql - start_postgresql
script_time_taken = end_script - start_script

You than have pg_time_taken and script_time_taken (which are
timedelta objects) that you can use for whatever display purposes
you need.

Alternatively, you can use time.clock()

from time import clock
clock()
# do stuff
start_pg = clock()
# do pg stuff
end_pg = clock()
# rest of script
end_script = clock()
print "Your script took %i seconds" % end_script
print "Your PG took %i seconds" % (end_pg - start_pg)

-tkc