Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Time python scripts using IPython magic

How can I time the execution of a Python script using the iPython %time or %%timeit magic commands? For example, I have script.py and I'd like to know how long it takes to execute. Small nuance: script.py needs input parameter(s). The below doesn't seem to work.

%%time script.py input_param1 input_param2
like image 812
user1507844 Avatar asked Jul 28 '14 22:07

user1507844


2 Answers

Solution

Your can use:

%%timeit
%run script.py input_param1 input_param2

beware that the script will be executed multiple times (the number is adaptive). To execute it only once (and have less accurate timing) change the first line to

%%timeit -n1 -r1

Explanation

All the magic commands starting with %% apply to the whole cell. In particular %%timeit will time all the lines in the cell.

IPython allows to use magic commands (single %) in any point of your code (i.e. loops, if-then). Here we just use the magic command %run to run the script.

See also: Magic functions from the official IPython docs.

like image 163
user2304916 Avatar answered Oct 18 '22 18:10

user2304916


You can also try cProfile which is a standard builtin Python profiler that is recommended for most users.

It gives both total running time in addition to the total run time of each function and the number of times each function was called.

Here is an example of how to use it when running your script. The results are saved to a file named 'output_stats':

import cProfile
import pstats

cProfile.run(open('primes.py', 'rb'), 'output_stats')

p = pstats.Stats('output_stats')

p.sort_stats('cumulative').print_stats(10)
Thu May 14 09:26:09 2015    output_stats
     369 function calls in 0.213 seconds

   Ordered by: cumulative time
   List reduced from 89 to 10 due to restriction <10>
   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.000    0.000    0.213    0.213 primes.py:1(<module>)
        1    0.019    0.019    0.213    0.213 primes.py:22(prime)
        2    0.141    0.070    0.181    0.091 primes.py:1(primes)
        3    0.041    0.014    0.041    0.014 {range}
        4    0.000    0.000    0.013    0.003 /usr/local/miniconda/envs/dev/lib/python2.7/site-packages/IPython/kernel/zmq/iostream.py:207(write)
        1    0.000    0.000    0.010    0.010 /usr/local/miniconda/envs/dev/lib/python2.7/site-packages/IPython/kernel/zmq/iostream.py:151(flush)
        1    0.000    0.000    0.010    0.010 /usr/local/miniconda/envs/dev/lib/python2.7/site-packages/IPython/kernel/zmq/session.py:589(send)
        1    0.000    0.000    0.009    0.009 /usr/local/miniconda/envs/dev/lib/python2.7/site-packages/IPython/kernel/zmq/session.py:530(serialize)
        4    0.000    0.000    0.007    0.002 /usr/local/miniconda/envs/dev/lib/python2.7/site-packages/IPython/kernel/zmq/session.py:84(<lambda>)
        4    0.000    0.000    0.007    0.002 /usr/local/miniconda/envs/dev/lib/python2.7/site-packages/zmq/utils/jsonapi.py:31(dumps)

===

Example script file named primes.py:

def primes(n): 
    if n == 2:
        return [2]
    elif n < 2:
        return []
    s=range(3, n + 1, 2)
    mroot = n ** 0.5
    half=(n + 1) / 2 - 1
    i = 0
    m = 3
    while m <= mroot:
        if s[i]:
            j = (m * m - 3) / 2
            s[j] = 0
            while j < half:
                s[j] = 0
                j += m
        i = i + 1
        m = 2 * i + 3
    return [2] + [x for x in s if x]

def prime(a, b):
    print(primes(a))
    print(primes(b))

if __name__ == "__main__":
    prime(10, 100)
like image 33
Alexander Avatar answered Oct 18 '22 18:10

Alexander