python - ipython 安装到使用

为什么你一定要用IPython?
ipython官方文档

我喜欢较真,所以前面的文章中,我多次的使用:

import time
ss=time.time()
...
print(time.time()-ss)

内置的time函数统计运行时间,在上一篇枚举买鸡里面,我在其他博主的文中看到了%%time这个神奇的代码,于是装!

算法设计思想(1)—— 穷举法

一路下来,净是蒙。
ipython是shell,但是我不知道shell到底是啥,于是装上ipython后,一顿瞎操作,无果。
以下是正确流程!

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple ipython
ipython

python - ipython 安装到使用_第1张图片python - ipython 安装到使用_第2张图片
于是,ipython并不是库,而是一个shell,至于她的易用性,不言而喻。她的输出效率,之后我会测试并补上!

另吹一波参考文章1里面的链接里面的两个软件,那个截图贴图太爱了!Snipaste、Screen2Gif

python - ipython 安装到使用_第3张图片
python - ipython 安装到使用_第4张图片很遗憾,那么舒适的字体,并没有µ符号!

还有个问题:

In [65]: for a in range(10):
    ...:     ss=time.time()
    ...:     for i in range(10000):
    ...:         for c in range(51):
    ...:             for r in range(51):
    ...:                 if c+r==50 and c*2+r*4==120:
    ...:                     ""
    ...:     print(time.time()-ss)
    ...:
2.326673746109009
2.249720811843872
2.253481149673462
2.250256061553955
2.264514446258545
2.2272815704345703
2.2063546180725098
2.2491822242736816
2.2210605144500732
2.2331531047821045

In [66]: %%timeit
    ...: for a in range(10):
    ...:     ss=time.time()
    ...:     for i in range(10000):
    ...:         for c in range(51):
    ...:             for r in range(51):
    ...:                 if c+r==50 and c*2+r*4==120:
    ...:                     ""
    ...:     print(time.time()-ss)
    ...:
1.2790648937225342
1.3098676204681396
1.3039336204528809
1.3092503547668457
1.321321964263916
1.2725393772125244
1.3208136558532715
1.3294315338134766
1.2868247032165527

emmmm
实际情况=%%time
而%%timeit则是很高的效率
所以两者不能跨越参考,前者统计实际运行,后者验证算法优劣!

%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

: 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. Changed in version 7.3: User variables are no longer expanded, the magic line is always left unmodified. Examples In [1]: %timeit pass 8.26 ns ± 0.12 ns per loop (mean ± std. dev. of 7 runs, 100000000 loops each) In [2]: u = None In [3]: %timeit u is None 29.9 ns ± 0.643 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each) In [4]: %timeit -r 4 u == None In [5]: import time In [6]: %timeit -n1 time.sleep(2) 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.

你可能感兴趣的:(python)