简体   繁体   English

时间python脚本使用IPython魔术

[英]Time python scripts using IPython magic

How can I time the execution of a Python script using the iPython %time or %%timeit magic commands? 如何使用iPython%time或%% timeit magic命令计时执行Python脚本? For example, I have script.py and I'd like to know how long it takes to execute. 例如,我有script.py,我想知道执行需要多长时间。 Small nuance: script.py needs input parameter(s). 细微差别:script.py需要输入参数。 The below doesn't seem to work. 以下似乎不起作用。

%%time script.py input_param1 input_param2

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. 特别是%%timeit将计时单元格中的所有行。

IPython allows to use magic commands (single % ) in any point of your code (ie loops, if-then). IPython允许在代码的任何一点使用魔术命令(单个% )(即循环,if-then)。 Here we just use the magic command %run to run the script. 这里我们只使用magic命令%run来运行脚本。

See also: Magic functions from the official IPython docs. 另请参阅:官方IPython文档中的魔术功能

You can also try cProfile which is a standard builtin Python profiler that is recommended for most users. 您还可以尝试cProfile ,它是大多数用户推荐的标准内置Python分析器。

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': 结果将保存到名为“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: 示例名为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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM