I have a command line program in Python that takes a while to finish. I want to know the exact time it takes to finish running.
I've looked at the timeit
module, but it seems it's only for small snippets of code. I want to time the whole program.
I have a command line program in Python that takes a while to finish. I want to know the exact time it takes to finish running.
I've looked at the timeit
module, but it seems it's only for small snippets of code. I want to time the whole program.
I really like Paul McGuire's answer, but I use Python3. So for those who are interested: here's a modification of his answer that works with Python 3 on *nix (I imagine, under Windows, that clock() should be used instead of time()):
If you find this useful, you should still up-vote his answer instead of this one, as he did most of the work ;).
I like the output the
datetime
module provides, where time delta objects show days, hours, minutes etc. as necessary in a human-readable way.For example:
Sample output e.g.
or
Update: As J.F. Sebastian mentioned, this approach might encounter some tricky cases with local time, so it's safer to use:
I like Paul McGuire's answer too and came up with a context manager form which suited more my needs.
In Linux or UNIX:
In Windows, see this Stackoverflow discussion: How to measure execution time of command in windows command line?
The time of a Python program's execution measure could be inconsistent depending on:
This is because the most effective way is using the "Order of Growth" and learn the Big "O" notation to do it properly, https://en.wikipedia.org/wiki/Big_O_notation
Anyway you can try to evaluate the performance of any Python program in specific machine counting steps per second using this simple algorithm: adapt this to the program you want to evaluate
Hope this help you.
There is a
timeit
module which can be used to time the execution times of python codes. It has detailed documentation and examples in python docs (https://docs.python.org/2/library/timeit.html)