-4

How do I measure the execution time of a python script without adding the code everytime like import timeit (start time - end time) in the script. I mean what can be done only once to show the execution time whenever i run a script.

Deepak
  • 1
  • 1
  • 3
  • 2
    You can use ipython's %timeit magic to time single statements. Have a look at https://stackoverflow.com/questions/29280470/what-is-timeit-in-python. If you want the execution time for the entire script, you can just use the `time` command on the terminal https://unix.stackexchange.com/questions/52313/how-to-get-execution-time-of-a-script-effectively – lakshayg Sep 02 '18 at 07:03

1 Answers1

0

As far as my knowledge goes, it's not possible to show the time of a python script without adding the code(start time- end time). But you can show the execution time of a python script like this:

from datetime import datetime
startTime = datetime.now()
#You can do anyting here
print("Hello World")
print(datetime.now() - startTime)

If you are on Linux, you can try:

time ./script.py

This works under OSX. This also works under cygwin on Windows. Though I haven't personally tested it because I am on a Linux machine.

Hope it helps :)

Mahir Islam
  • 1,941
  • 2
  • 12
  • 33