19

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
4
  • 2
    As a side note, why not refactor script.py so that it can be used as a module (so you can import it and then script.run(input_param1, input_param2) does the same thing as if those two values were sys.argv[1:3] on the command line)? That would probably help for all kinds of testing, not just performance testing… Commented Jul 28, 2014 at 23:14
  • @abarnert do you mind giving a quick example of what it would look like to refactor script.py as a module? Commented Dec 6, 2015 at 23:02
  • Refactoring a script to work also as a module can be done by placing the code within functions (and also including a statement if __name__ == '__main__': main() (with newline after the :, as recommended style; here I am writing within a comment, so I cannot insert a newline) if the code will ever be run as a script), as described in the Python documentation. Commented Apr 16, 2021 at 14:55
  • This question is similar to: How to run IPython magic from a script. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. Commented Apr 6 at 2:21

2 Answers 2

39

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.