Script Running Time
Tim Chase
python.list at tim.thechases.com
Thu Feb 21 14:19:53 EST 2008
> 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
More information about the Python-list
mailing list