Skip to content Skip to sidebar Skip to footer

Easiest Way To Calculate Execution Time Of A Python Script?

What's the easiest way to calculate the execution time of a Python script?

Solution 1:

timeit module is designed specifically for this purpose.

Silly example as follows

deftest():
    """Stupid test function"""
    L = []
    for i inrange(100):
        L.append(i)

if __name__ == '__main__':
    from timeit import Timer
    t = Timer("test()", "from __main__ import test")
    print t.timeit()

Note that timeit can also be used from the command line (python -m timeit -s 'import module' 'module.test()') and that you can run the statement several times to get a more accurate measurement. Something I think time command doesn't support directly. -- jcollado

Solution 2:

Using Linux time command like this : time python file.py

Or you can take the times at start and at end and calculate the difference.

Solution 3:

Under linux: time python script.py

Solution 4:

How about using time? example: time myPython.py

Post a Comment for "Easiest Way To Calculate Execution Time Of A Python Script?"