In [2]: run examples/ipython_bug.py---------------------------------------------------------------------------AssertionErrorTraceback (most recent call last)/home/wesm/code/pydata-book/examples/ipython_bug.py in<module>()13throws_an_exception()14---> 15calling_things()/home/wesm/code/pydata-book/examples/ipython_bug.py incalling_things()11defcalling_things():12works_fine()---> 13throws_an_exception()1415calling_things()/home/wesm/code/pydata-book/examples/ipython_bug.py inthrows_an_exception()7 a =58 b =6---->9assert(a + b ==10)1011defcalling_things():AssertionError:In [3]:%debug>/home/wesm/code/pydata-book/examples/ipython_bug.py(9)throws_an_exception()8 b =6---->9assert(a + b ==10)10ipdb>
In [5]: run -d examples/ipython_bug.pyBreakpoint 1 at /home/wesm/code/pydata-book/examples/ipython_bug.py:1NOTE: Enter 'c' at the ipdb> prompt to start your script.><string>(1)<module>()ipdb> s--Call-->/home/wesm/code/pydata-book/examples/ipython_bug.py(1)<module>()1---> 1defworks_fine():2 a =53 b =6
ipdb> b 12ipdb> c>/home/wesm/code/pydata-book/examples/ipython_bug.py(12)calling_things()11defcalling_things():2-->12works_fine()13throws_an_exception()
ipdb> s--Call-->/home/wesm/code/pydata-book/examples/ipython_bug.py(6)throws_an_exception()5---->6defthrows_an_exception():7 a =5ipdb> n>/home/wesm/code/pydata-book/examples/ipython_bug.py(7)throws_an_exception()6defthrows_an_exception():---->7 a =58 b =6ipdb> n>/home/wesm/code/pydata-book/examples/ipython_bug.py(8)throws_an_exception()7 a =5---->8 b =69assert(a + b ==10)ipdb> n>/home/wesm/code/pydata-book/examples/ipython_bug.py(9)throws_an_exception()8 b =6---->9assert(a + b ==10)10ipdb> !a5ipdb> !b6
In [1]:%run -d examples/ipython_bug.pyBreakpoint 1 at /home/wesm/code/pydata-book/examples/ipython_bug.py:1NOTE: Enter 'c' at the ipdb> prompt to start your script.><string>(1)<module>()ipdb>
加上-b和行号,可以预设一个断点:
In [2]:%run -d -b2 examples/ipython_bug.pyBreakpoint 1 at /home/wesm/code/pydata-book/examples/ipython_bug.py:2NOTE: Enter 'c' at the ipdb> prompt to start your script.><string>(1)<module>()ipdb> c>/home/wesm/code/pydata-book/examples/ipython_bug.py(2)works_fine()1defworks_fine():1---> 2 a =53 b =6ipdb>
# a very large list of stringsstrings = ['foo','foobar','baz','qux','python','Guido Van Rossum'] *100000method1 = [x for x in strings if x.startswith('foo')]method2 = [x for x in strings if x[:3]=='foo']
看起来它们的性能应该是同级别的,但事实呢?用%time进行一下测量:
In [561]:%time method1 = [x for x in strings if x.startswith('foo')]CPU times: user 0.19 s, sys:0.00 s, total:0.19 sWall time:0.19 sIn [562]:%time method2 = [x for x in strings if x[:3]=='foo']CPU times: user 0.09 s, sys:0.00 s, total:0.09 sWall time:0.09 s
In [563]:%timeit [x for x in strings if x.startswith('foo')]10 loops, best of 3:159 ms per loopIn [564]:%timeit [x for x in strings if x[:3]=='foo']10 loops, best of 3:59.3 ms per loop
In [565]: x ='foobar'In [566]: y ='foo'In [567]:%timeit x.startswith(y)1000000 loops, best of 3:267 ns per loopIn [568]:%timeit x[:3]== y10000000 loops, best of 3:147 ns per loop
from numpy.random import randndefadd_and_sum(x,y): added = x + y summed = added.sum(axis=1)return summeddefcall_function(): x =randn(1000, 1000) y =randn(1000, 1000)returnadd_and_sum(x, y)
如果想了解add_and_sum函数的性能,%prun可以给出下面内容:
In [569]:%run prof_modIn [570]: x =randn(3000, 3000)In [571]: y =randn(3000, 3000)In [572]:%prun add_and_sum(x, y)4 function calls in0.049 seconds Ordered by: internal time ncalls tottime percall cumtime percall filename:lineno(function)10.0360.0360.0460.046 prof_mod.py:3(add_and_sum)10.0090.0090.0090.009{method 'sum' of 'numpy.ndarray'}10.0030.0030.0490.049<string>:1(<module>)
In [573]:%lprun -f add_and_sum add_and_sum(x, y)Timer unit:1e-06 sFile: prof_mod.pyFunction: add_and_sum at line 3Total time:0.045936 sLine # Hits Time Per Hit % Time Line Contents==============================================================3defadd_and_sum(x,y):413651036510.079.5 added = x + y5194259425.020.5 summed = added.sum(axis=1)6111.00.0return summed
In [574]:%lprun -f add_and_sum -f call_function call_function()Timer unit:1e-06 sFile: prof_mod.pyFunction: add_and_sum at line 3Total time:0.005526 sLine # Hits Time Per Hit % Time Line Contents==============================================================3defadd_and_sum(x,y):4143754375.079.2 added = x + y5111491149.020.8 summed = added.sum(axis=1)6122.00.0return summedFile: prof_mod.pyFunction: call_function at line 8Total time:0.121016 sLine # Hits Time Per Hit % Time Line Contents==============================================================8defcall_function():915716957169.047.2 x =randn(1000, 1000)1015830458304.048.2 y =randn(1000, 1000)11155435543.04.6returnadd_and_sum(x, y)
classMessage:def__init__(self,msg): self.msg = msgdef__repr__(self):return'Message: %s'% self.msgIn [579]: x =Message('I have a secret')In [580]: xOut[580]: Message: I have a secret
$ ipython --profile=secret_project
Python 3.5.1 | packaged by conda-forge | (default, May 20 2016, 05:22:56)
Type "copyright", "credits" or "license" for more information.
IPython 5.1.0 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
IPython profile: secret_project