# 常见 Python 代码优化技巧

O(1) -> O(lg n) -> O(n lg n) -> O(n^2) -> O(n^3) -> O(n^k) -> O(k^n) -> O(n!)

Python 字典中使用了 hash table，因此查找操作的复杂度为 O(1)，而 list 实际是个数组，在 list 中，查找需要遍历整个 list，其复杂度为 O(n)，因此对成员的查找访问等操作字典要比 list 更快。

```from time import time
t = time()
list = [ a , b , is , python , jason , hello , hill , with , phone , test ,
dfdf , apple , pddf , ind , basic , none , baecr , var , bana , dd , wrd ]
#list = dict.fromkeys(list,True)
print list
filter = []
for i in range (1000000):
for find in [ is , hat , new , list , old , . ]:
if find not in list:
filter.append(find)
print "total run time:"
print time()-t```

set 的 union， intersection，difference 操作要比 list 的迭代要快。因此如果涉及到求 list 交集，并集或者差的问题可以转换为 set 来操作。

```from time import time
t = time()
lista=[1,2,3,4,5,6,7,8,9,13,34,53,42,44]
listb=[2,4,6,9,23]
intersection=[]
for i in range (1000000):
for a in lista:
for b in listb:
if a == b:
intersection.append(a)

print "total run time:"
print time()-t```

```total run time:
38.4070000648```

```from time import time
t = time()
lista=[1,2,3,4,5,6,7,8,9,13,34,53,42,44]
listb=[2,4,6,9,23]
intersection=[]
for i in range (1000000):
list(set(lista)&set(listb))
print "total run time:"
print time()-t```

set(list1) & set(list2) intersection 包含 list1 和 list2 中共同元素的新集合

set(list1) - set(list2) difference 在 list1 中出现但不在 list2 中出现的元素的集合

```from time import time
t = time()
lista = [1,2,3,4,5,6,7,8,9,10]
listb =[0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,0.01]
for i in range (1000000):
for a in range(len(lista)):
for b in range(len(listb)):
x=lista[a]+listb[b]
print "total run time:"
print time()-t```

```from time import time
t = time()
lista = [1,2,3,4,5,6,7,8,9,10]
listb =[0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,0.01]
len1=len(lista)
len2=len(listb)
for i in xrange (1000000):
for a in xrange(len1):
temp=lista[a]
for b in xrange(len2):
x=temp+listb[b]
print "total run time:"
print time()-t```

python 中条件表达式是 lazy evaluation 的，也就是说如果存在条件表达式 if x and y，在 x 为 false 的情况下 y 表达式的值将不再计算。因此可以利用该特性在一定程度上提高程序效率。

```from time import time
t = time()
abbreviations = [ cf. ,  e.g. ,  ex. ,  etc. ,  fig. ,  i.e. ,  Mr. ,  vs. ]
for i in range (1000000):
for w in ( Mr. ,  Hat ,  is ,  chasing ,  the ,  black ,  cat ,  . ):
if w in abbreviations:
#if w[-1] ==  .  and w in abbreviations:
pass
print "total run time:"
print time()-t```

python 中的字符串对象是不可改变的，因此对任何字符串的操作如拼接，修改等都将产生一个新的字符串对象，而不是基于原字符串，因此这种持续的 copy 会在一定程度上影响 python 的性能。对字符串的优化也是改善性能的一个重要的方面，特别是在处理文本较多的情况下。字符串的优化主要集中在以下几个方面：

```from time import time

t = time()
s = ""
list = [ a , b , b , d , e , f , g , h , i , j , k , l , m , n ]
for i in range (10000):
for substr in list:
s+= substr
print "total run time:"
print time()-t```

```s = ""
for x in list:
s += func(x)```

```slist = [func(elt) for elt in somelist]
s = "".join(slist)```

`out = "%s%s%s%s" % (head, prologue, query, tail)`

`out = "" + head + prologue + query + tail + ""`

```from time import time
t = time()
list = [ a , b , is , python , jason , hello , hill , with , phone , test ,
dfdf , apple , pddf , ind , basic , none , baecr , var , bana , dd , wrd ]
total=[]
for i in range (1000000):
for w in list:
total.append(w)
print "total run time:"
print time()-t```

```for i in range (1000000):
a = [w for w in list]```

```>>> from timeit import Timer
>>> Timer("t=a;a=b;b=t","a=1;b=2").timeit()
0.25154118749729365
>>> Timer("a,b=b,a","a=1;b=2").timeit()
0.17156677734181258
>>>```

profile 的使用非常简单，只需要在使用之前进行 import 即可。具体实例如下：

```import profile
def profileTest():
Total =1;
for i in range(10):
Total=Total*(i+1)
print Total
if __name__ == "__main__":
profile.run("profileTest()")```

```ncalls：表示函数调用的次数；
tottime：表示指定函数的总的运行时间，除掉函数中调用子函数的运行时间；
percall：（第一个 percall）等于 tottime/ncalls；
cumtime：表示该函数及其所有子函数的调用运行的时间，即函数开始调用到返回的时间；
percall：（第二个 percall）即函数运行一次的平均时间，等于 cumtime/ncalls；
filename:lineno(function)：每个函数调用的具体信息；```

```import pstats
p = pstats.Stats( testprof )
p.sort_stats("name").print_stats()```

Python 性能优化工具

Python 性能优化除了改进算法，选用合适的数据结构之外，还有几种关键的技术，比如将关键 python 代码部分重写成 C 扩展模块，或者选用在性能上更为优化的解释器等，这些在本文中统称为优化工具。python 有很多自带的优化工具，如 Psyco，Pypy，Cython，Pyrex 等，这些优化工具各有千秋，本节选择几种进行介绍。

Psyco

psyco 是一个 just-in-time 的编译器，它能够在不改变源代码的情况下提高一定的性能，Psyco 将操作编译成有点优化的机器码，其操作分成三个不同的级别，有"运行时"、"编译时"和"虚拟时"变量。并根据需要提高和降低变量的级别。运行时变量只是常规 Python 解释器处理的原始字节码和对象结构。一旦 Psyco 将操作编译成机器码，那么编译时变量就会在机器寄存器和可直接访问的内存位置中表示。同时 python 能高速缓存已编译的机器码以备今后重用，这样能节省一点时间。但 Psyco 也有其缺点，其本身运行所占内存较大。目前 psyco 已经不在 python2.7 中支持，而且不再提供维护和更新了，对其感兴趣的可以参考 http://psyco.sourceforge.net/

Pypy

PyPy 表示 "用 Python 实现的 Python"，但实际上它是使用一个称为 RPython 的 Python 子集实现的，能够将 Python 代码转成 C， .NET， Java 等语言和平台的代码。PyPy 集成了一种即时 (JIT) 编译器。和许多编译器，解释器不同，它不关心 Python 代码的词法分析和语法树。因为它是用 Python 语言写的，所以它直接利用 Python 语言的 Code Object.。Code Object 是 Python 字节码的表示，也就是说， PyPy 直接分析 Python 代码所对应的字节码 ,，这些字节码即不是以字符形式也不是以某种二进制格式保存在文件中， 而在 Python 运行环境中。目前版本是 1.8. 支持不同的平台安装，windows 上安装 Pypy 需要先下载 https://bitbucket.org/pypy/pypy/downloads/pypy-1.8-win32.zip，然后解压到相关的目录，并将解压后的路径添加到环境变量 path 中即可。在命令行运行 pypy，如果出现如下错误："没有找到 MSVCR100.dll, 因此这个应用程序未能启动，重新安装应用程序可能会修复此问题"，则还需要在微软的官网上下载 VS 2010 runtime libraries 解决该问题。具体地址为 http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=5555

```C:Documents and SettingsAdministrator>pypy
Python 2.7.2 (0e28b379d8b3, Feb 09 2012, 18:31:47)
[PyPy 1.8.0 with MSC v.1500 32 bit] on win32
And now for something completely different: ``PyPy is vast, and contains
multitudes
>>>>```

```C:Documents and SettingsAdministrator 桌面 docpython>pypy loop.py
total run time:
8.42199993134

C:Documents and SettingsAdministrator 桌面 docpython>python loop.py
total run time:
106.391000032```

Cython

Cython 是用 python 实现的一种语言，可以用来写 python 扩展，用它写出来的库都可以通过 import 来载入，性能上比 python 的快。cython 里可以载入 python 扩展 ( 比如 import math)，也可以载入 c 的库的头文件 ( 比如 :cdef extern from "math.h")，另外也可以用它来写 python 代码。将关键部分重写成 C 扩展模块

Linux Cpython 的安装： 第一步：下载

```[root@v5254085f259 cpython]# wget -N http://cython.org/release/Cython-0.15.1.zip
--2012-04-16 22:08:35--  http://cython.org/release/Cython-0.15.1.zip
Resolving cython.org... 128.208.160.197
Connecting to cython.org|128.208.160.197|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 2200299 (2.1M) [application/zip]
Saving to: `Cython-0.15.1.zip

100%[======================================>] 2,200,299   1.96M/s   in 1.1s

2012-04-16 22:08:37 (1.96 MB/s) - `Cython-0.15.1.zip  saved [2200299/2200299]```

`[root@v5254085f259 cpython]# unzip -o Cython-0.15.1.zip`

`python setup.py install`

```[root@v5254085f259 Cython-0.15.1]# cython
Cython (http://cython.org) is a compiler for code written in the
Cython language.  Cython is based on Pyrex by Greg Ewing.

Usage: cython [options] sourcefile.{pyx,py} ...

Options:
-V, --version                  Display version number of cython compiler
-l, --create-listing           Write error messages to a listing file
-I, --include-dir   Search for include files in named directory
(multiple include directories are allowed).
-o, --output-file    Specify name of generated C file
-t, --timestamps               Only compile newer source files
-f, --force                    Compile all source files (overrides implied -t)
-q, --quiet                    Don t print module names in recursive mode
-v, --verbose                  Be verbose, print file names on multiple compil ation
-p, --embed-positions          If specified, the positions in Cython files of each
function definition is embedded in its docstring.
--cleanup
Release interned objects on python exit, for memory debugging.
Level indicates aggressiveness, default 0 releases nothing.
-w, --working
Sets the working directory for Cython (the directory modules are searched from)
--gdb Output debug information for cygdb
-D, --no-docstrings
Strip docstrings from the compiled module.
-a, --annotate
Produce a colorized HTML version of the source.
--line-directives
Produce #line directives pointing to the .pyx source
--cplus
Output a C++ rather than C file.
--embed[=]
Generate a main() function that embeds the Python interpreter.
-2          Compile based on Python-2 syntax and code seman tics.
-3          Compile based on Python-3 syntax and code seman tics.
--fast-fail     Abort the compilation on the first error
--warning-error, -Werror       Make all warnings into errors
--warning-extra, -Wextra       Enable extra warnings
-X, --directive =
[,<name=value,...] Overrides a compiler directive```

Cython 代码与 python 不同，必须先编译，编译一般需要经过两个阶段，将 pyx 文件编译为 .c 文件，再将 .c 文件编译为 .so 文件。编译有多种方法：

```def sum(int a,int b):
print a+b

[root@v5254085f259 test]# cython sum.pyx
[root@v5254085f259 test]# ls
total 76
4 drwxr-xr-x 2 root root  4096 Apr 17 02:45 .
4 drwxr-xr-x 4 root root  4096 Apr 16 22:20 ..
4 -rw-r--r-- 1 root root    35 Apr 17 02:45 1
60 -rw-r--r-- 1 root root 55169 Apr 17 02:45 sum.c
4 -rw-r--r-- 1 root root    35 Apr 17 02:45 sum.pyx```

```[root@v5254085f259 test]# gcc -shared -pthread -fPIC -fwrapv -O2
-Wall -fno-strict-aliasing -I/usr/include/python2.4 -o sum.so sum.c
[root@v5254085f259 test]# ls
total 96
4 drwxr-xr-x 2 root root  4096 Apr 17 02:47 .
4 drwxr-xr-x 4 root root  4096 Apr 16 22:20 ..
4 -rw-r--r-- 1 root root    35 Apr 17 02:45 1
60 -rw-r--r-- 1 root root 55169 Apr 17 02:45 sum.c
4 -rw-r--r-- 1 root root    35 Apr 17 02:45 sum.pyx
20 -rwxr-xr-x 1 root root 20307 Apr 17 02:47 sum.so```

```from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext

ext_modules = [Extension("sum", ["sum.pyx"])]

setup(
name =  sum app ,
cmdclass = { build_ext : build_ext},
ext_modules = ext_modules
)

[root@v5254085f259 test]#  python setup.py build_ext --inplace
running build_ext
cythoning sum.pyx to sum.c
building  sum  extension
gcc -pthread -fno-strict-aliasing -fPIC -g -O2 -DNDEBUG -g -fwrapv -O3
-Wall -Wstrict-prototypes -fPIC -I/opt/ActivePython-2.7/include/python2.7
-c sum.c -o build/temp.linux-x86_64-2.7/sum.o
gcc -pthread -shared build/temp.linux-x86_64-2.7/sum.o
-o /root/cpython/test/sum.so```

```[root@v5254085f259 test]# python
ActivePython 2.7.2.5 (ActiveState Software Inc.) based on
Python 2.7.2 (default, Jun 24 2011, 11:24:26)
[GCC 4.0.2 20051125 (Red Hat 4.0.2-8)] on linux2
>>> import pyximport; pyximport.install()
>>> import sum
>>> sum.sum(1,3)```

```from time import time
def test(int n):
cdef int a =0
cdef int i
for i in xrange(n):
a+= i
return a

t = time()
test(10000000)
print "total run time:"
print time()-t```

```[GCC 4.0.2 20051125 (Red Hat 4.0.2-8)] on linux2
>>> import pyximport; pyximport.install()
>>> import ctest
total run time:
0.00714015960693```

```from time import time
def test(n):
a =0;
for i in xrange(n):
a+= i
return a

t = time()
test(10000000)
print "total run time:"
print time()-t

[root@v5254085f259 test]# python test.py
total run time:
0.971596002579```

880 篇文章146 人订阅

0 条评论