0

I'm running scripts that take 10-80 minutes. I would like to be able to print the script running time each 1/5/10 minutes ( as i choose).

For example i have a script where i'm creating tables and inserting data to a DB and want to measure the time through the script.

So lets say i have some printing msgs like "Table 1 has been created" , "Data inserted to Table 2" etc.

Between those msgs i want to add like: The script is running for 1 minutes The script is running for 2 minutes The script is running for 3 minutes Etc...

Someone know what is the best practice for it ?

Thanks in advance !

shayms8
  • 671
  • 6
  • 13
  • 28
  • You might take a look at the contextmanager as described in [this question](http://stackoverflow.com/q/30828495/3991125). – albert Jul 19 '16 at 11:58

1 Answers1

1

You can use datetime:

from datetime import datetime
#at the start of the script:
start_time = datetime.now()
# ... some stuff ...
# when you want to print the time elapsed so far:
now_time = datetime.now()
print(now_time - start_time)

(of course you can reformat the printing as you wish)

Ohad Eytan
  • 8,114
  • 1
  • 22
  • 31
  • Hey, This is not bad. BUT, I wanted to run it parallel to the script and print the time each 10 min, and not only when i write `print (time calculation)` – shayms8 Jul 20 '16 at 11:57