IPython(jupyter环境)中的”魔法“命令 %time,%timeit,%%time,%%timeit

IPython(jupyter环境)中的”魔法“命令 %time,%timeit,%%time,%%timeit

前言:

这一部分原打算写在应该自己的Python OpenCV速度提升全汇总这篇博客里的,但这些命令只是单纯用来分析代码时间。如果加在那篇代码里的话,可能就会显得太冗长了。所以单独写出来了。

正文

个人理解

%代表着线模式,%%代表单元格模式。
%time和%%time代表的是这个语句执行这一次所用的时间
%timeit和%%timeit代表的是这个语句多次执行的单次平均时间。所以timeit更精准可控,而time更方便。

官方详细介绍

原文搬运,非常推荐大家去这里
IPython官方文档内置魔术命令的介绍
看原文,当然这两个部分我先搬运过来,方便大家理解哈。如果不想看这些,可以直接看下一节举例说明,那里会清晰详细的解释给大家看

time

Time execution of a Python statement or expression.

The CPU and wall clock times are printed, and the value of the expression (if any) is returned. Note that under Win32, system time is always reported as 0, since it can not be measured.

This function can be used both as a line and cell magic:

In line mode you can time a single-line statement (though multiple ones can be chained with using semicolons).

In cell mode, you can time the cell body (a directly following statement raises an error).

This function provides very basic timing functionality. Use the timeit magic for more control over the measurement.

扫描二维码关注公众号,回复: 10844500 查看本文章

timeit

Time execution of a Python statement or expression

Usage, in line mode:
%timeit [-n -r [-t|-c] -q -p

-o] statement

or in cell mode:
%%timeit [-n -r [-t|-c] -q -p

-o] setup_code code code…

Time execution of a Python statement or expression using the timeit module. This function can be used both as a line and cell magic:

In line mode you can time a single-line statement (though multiple ones can be chained with using semicolons).

In cell mode, the statement in the first line is used as setup code (executed but not timed) and the body of the cell is timed. The cell body has access to any variables created in the setup code.

Options: -n: execute the given statement times in a loop. If is not provided, is determined so as to get sufficient accuracy.

-r: number of repeats , each consisting of loops, and take the best result. Default: 7

-t: use time.time to measure the time, which is the default on Unix. This function measures wall time.

-c: use time.clock to measure the time, which is the default on Windows and measures wall time. On Unix, resource.getrusage is used instead and returns the CPU user time.

-p< P>: use a precision of

digits to display the timing result. Default: 3

-q: Quiet, do not print result.

-o: return a TimeitResult that can be stored in a variable to inspect
the result in more details.

The times reported by %timeit will be slightly higher than those reported by the timeit.py script when variables are accessed. This is due to the fact that %timeit executes the statement in the namespace of the shell, compared with timeit.py, which uses a single setup statement to import function or create variables. Generally, the bias does not matter as long as results from timeit.py are not mixed with those from %timeit.

举例说明

再用例子来解释下上面的文档。
首先是无关的例子,经常使用的time模块。

import time
t1=time.time()
test_work=[x**2 for x in range(1,1000000,3)]
t2=time.time()
print(t2-t1)

输出
无关例子
好的,我们步入主题。

time

这个比较简单

%time test_work=[x**2 for x in range(1,1000000,3)]

输出
%time例子

%%time
test_work=[x**2 for x in range(1,1000000,3)]
test_work=[x**2 for x in range(1,1000000,3)]
test_work=[x**2 for x in range(1,1000000,3)]

输出
%%time例子
time模块的话会返回两个时间,一个是CPU times,另一个是Wall time。
如果你对CPU times和Wall time。不太理解。建议看这篇博客。
CPU time与WALL time
文档指出在WIN32系统中sys时间总是为0,因为没法被测量。

timeit

%timeit [-n<N> -r<R> [-t|-c] -q -p<P> -o]#线模式
%%timeit [-n<N> -r<R> [-t|-c] -q -p<P> -o]#单元格模式

还是从最简单的开始

%timeit test_work=[x**2 for x in range(1,1000000,3)]

输出
%timeit最简单例子
接着我们加这些东西
先列出这些参数是啥。
-n :在循环中执行给定语句次。如果未提供,则确定以获得足够的精度。%timeit默认值为10,%%timeit的默认值为1
-r :重复次数,每个重复次数由个循环组成,并获得最佳结果。默认值均为7
-t:使用time.time来测量时间,这是Unix上的默认值。此功能测量墙壁时间。
-c:使用time.clock来测量时间,这是Windows上的默认设置,用于测量墙壁时间。在Unix上,使用resource.getrusage代替并返回CPU用户时间。
-p < P>:使用

位的精度显示计时结果。默认值均为:3
-q:安静,不打印结果。
-o:返回一个TimeitResult,可以将其存储在变量中以进行检查

我们先加-n和-r。

%timeit -n30 -r10 test_work=[x**2 for x in range(1,1000000,3)]

输出
%timeit只加n和r
上面那张图我们可以看到改动后对返回的值的影响
接着改动 -p 和 选择-t或者-c。

%timeit -n30 -r10 -p5 -c test_work=[x**2 for x in range(1,1000000,3)]

%timeit -n30 -r10 -p5 -t test_work=[x**2 for x in range(1,1000000,3)]

输出:-p-c-t
至于-q,我加了,果然真的quiet了。

接着说%%timeit.

%%timeit
test_work=[x**2 for x in range(1,1000000,3)]
test_work=[x**2 for x in range(1,1000000,3)]
test_work=[x**2 for x in range(1,1000000,3)]

输出
%%timeit例子
还是加-r -n -c/-t -p

%%timeit -n30 -r10
test_work=[x**2 for x in range(1,1000000,3)]
test_work=[x**2 for x in range(1,1000000,3)]
test_work=[x**2 for x in range(1,1000000,3)]

输出
在这里插入图片描述

%%timeit -n30 -r10 -p5 -c 
test_work=[x**2 for x in range(1,1000000,3)]
test_work=[x**2 for x in range(1,1000000,3)]
test_work=[x**2 for x in range(1,1000000,3)]

输出
在这里插入图片描述

%%timeit -n30 -r10 -p5 -t 
test_work=[x**2 for x in range(1,1000000,3)]
test_work=[x**2 for x in range(1,1000000,3)]
test_work=[x**2 for x in range(1,1000000,3)]

输出
在这里插入图片描述

结束语

感谢看到这里,不懂的地方欢迎留言。

发布了11 篇原创文章 · 获赞 38 · 访问量 4233

猜你喜欢

转载自blog.csdn.net/qq_37924224/article/details/104983047