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...depend ing on the resolution you need, you can do
something like
import datetime
start_script = datetime.dateti me.now()
# do stuff
start_postgresq l = datetime.dateti me.now()
# make your PG call
end_postgresql = datetime.dateti me.now()
# do remaining stuff
end_script = datetime.dateti me.now()
pg_time_taken = end_postgresql - start_postgresq l
script_time_tak en = end_script - start_script
You than have pg_time_taken and script_time_tak en (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