ensnare
ensnare

Reputation: 42113

Easiest way to calculate execution time of a python script?

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

Upvotes: 4

Views: 6682

Answers (4)

Jakob Bowyer
Jakob Bowyer

Reputation: 34718

timeit module is designed specifically for this purpose.

Silly example as follows

def test():
    """Stupid test function"""
    L = []
    for i in range(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

Upvotes: 8

Neilvert Noval
Neilvert Noval

Reputation: 1695

How about using time?
example: time myPython.py

Upvotes: 0

Cydonia7
Cydonia7

Reputation: 3846

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.

Upvotes: 5

Andrey Nikishaev
Andrey Nikishaev

Reputation: 3882

Under linux: time python script.py

Upvotes: 1

Related Questions