Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Easiest way to calculate execution time of a python script?

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

like image 524
ensnare Avatar asked Dec 10 '22 03:12

ensnare


2 Answers

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

like image 84
Jakob Bowyer Avatar answered Dec 28 '22 08:12

Jakob Bowyer


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.

like image 25
Cydonia7 Avatar answered Dec 28 '22 08:12

Cydonia7