代码优化

1 代码评估

1.1 timeit

使用timeit评估一个函数的的施行效率(IPython)


In [1]:
import numpy as np
a = np.arange(1000)
%timeit a**2


The slowest run took 131.59 times longer than the fastest. This could mean that an intermediate result is being cached.
1000000 loops, best of 3: 1.64 µs per loop

In [2]:
%timeit a**2.1


The slowest run took 4.59 times longer than the fastest. This could mean that an intermediate result is being cached.
10000 loops, best of 3: 32.7 µs per loop

In [3]:
%timeit a*a


The slowest run took 62.51 times longer than the fastest. This could mean that an intermediate result is being cached.
100000 loops, best of 3: 1.59 µs per loop

1.2 Profiler


In [4]:
% run -t ica.py


IPython CPU timings (estimated):
  User   :       6.45 s.
  System :       0.20 s.
Wall time:       3.80 s.

In [6]:
% run -p ica.py


 

1.3 Line-Profiler


In [12]:
import profile
from scipy import linalg
def test():
    data = np.random.random((5000,100))
    u,s,v = linalg.svd(data)
    pca = np.dot(u[:,:10].T,data)


1 loop, best of 3: 3.29 s per loop

2 代码跑的更快

2.1 使用更好的算法


In [13]:
data = np.random.random((5000,100))
%timeit linalg.svd(data)


1 loop, best of 3: 3.23 s per loop

In [15]:
%timeit linalg.svd(data,full_matrices=False)


10 loops, best of 3: 20.3 ms per loop

2.2 数值计算


In [18]:
a = np.zeros(1e7)
%timeit global a; a =0*a


/Users/gaufung/Anaconda/anaconda/lib/python2.7/site-packages/ipykernel/__main__.py:1: VisibleDeprecationWarning: using a non-integer number instead of an integer will result in an error in the future
  if __name__ == '__main__':
10 loops, best of 3: 28.9 ms per loop

In [19]:
a = np.zeros(1e7)
%timeit global a; a *= 0


/Users/gaufung/Anaconda/anaconda/lib/python2.7/site-packages/ipykernel/__main__.py:1: VisibleDeprecationWarning: using a non-integer number instead of an integer will result in an error in the future
  if __name__ == '__main__':
100 loops, best of 3: 9.08 ms per loop

In [ ]: