# 加载中...

bicloud
• 博客等级：
• 博客积分：0
• 博客访问：378,159
• 关注人气：492
• 获赠金笔：0支
• 赠出金笔：0支
• 荣誉徽章：

## python 代码性能分析

(2011-08-10 15:56:45)

### it

Ubuntu

#!/usr/bin/env python
import sys
import datetime
# area of space to investigate
x1, x2, y1, y2 = -2.13, 0.77, -1.3, 1.3

# Original code, prints progress (because it is slow)
# Uses complex datatype

def calculate_z_serial_purepython(q, maxiter, z):
"""Pure python with complex datatype, iterating over list of q and z"""
output = [0] * len(q)
for i in range(len(q)):
if i % 1000 == 0:
# print out some progress info since it is so slow...
print "%0.2f%% complete" % (1.0/len(q) * i * 100)
for iteration in range(maxiter):
z[i] = z[i]*z[i] + q[i]
if abs(z[i]) > 2.0:
output[i] = iteration
break
return output

def calc_pure_python(show_output):
# make a list of x and y values which will represent q
# xx and yy are the co-ordinates, for the default configuration they'll look like:
# if we have a 1000x1000 plot
# xx = [-2.13, -2.1242, -2.1184000000000003, ..., 0.7526000000000064, 0.7584000000000064, 0.7642000000000064]
# yy = [1.3, 1.2948, 1.2895999999999999, ..., -1.2844000000000058, -1.2896000000000059, -1.294800000000006]
x_step = (float(x2 - x1) / float(w)) * 2
y_step = (float(y1 - y2) / float(h)) * 2
x=[]
y=[]
ycoord = y2
while ycoord > y1:
y.append(ycoord)
ycoord += y_step
xcoord = x1
while xcoord < x2:
x.append(xcoord)
xcoord += x_step
q = []
for ycoord in y:
for xcoord in x:
q.append(complex(xcoord,ycoord))

z = [0+0j] * len(q)
print "Total elements:", len(z)
start_time = datetime.datetime.now()
output = calculate_z_serial_purepython(q, maxiter, z)
end_time = datetime.datetime.now()
secs = end_time - start_time
print "Main took", secs

validation_sum = sum(output)
print "Total sum of elements (for validation):", validation_sum

if show_output:
try:
import Image
import numpy as nm
output = nm.array(output)
output = (output + (256*output) + (256**2)*output) * 8
im = Image.new("RGB", (w/2, h/2))
im.fromstring(output.tostring(), "raw", "RGBX", 0, -1)
im.show()
except ImportError as err:
# Bail gracefully if we're using PyPy
print "Couldn't import Image or numpy:", str(err)

if __name__ == "__main__":
# get width, height and max iterations from cmd line
# 'python mandelbrot_pypy.py 100 300'
w = int(sys.argv[1]) # e.g. 100
h = int(sys.argv[1]) # e.g. 100
maxiter = int(sys.argv[2]) # e.g. 300

# we can show_output for Python, not for PyPy
calc_pure_python(True)

cProfile参考

afan@ubuntu:~/python-code\$ python -m cProfile -o rep.prof pure_python.py 1000 1000
Total elements: 250000
0.00% complete
0.40% complete
0.80% complete
1.20% complete
1.60% complete
2.00% complete
2.40% complete
2.80% complete
3.20% complete
3.60% complete
4.00% complete
4.40% complete
4.80% complete
5.20% complete
5.60% complete
6.00% complete
6.40% complete
6.80% complete
7.20% complete
7.60% complete
8.00% complete
8.40% complete
8.80% complete
9.20% complete
9.60% complete
10.00% complete
10.40% complete
10.80% complete
11.20% complete
11.60% complete
12.00% complete
12.40% complete
12.80% complete
13.20% complete
13.60% complete
14.00% complete
14.40% complete
14.80% complete
15.20% complete
15.60% complete
16.00% complete
16.40% complete
16.80% complete
17.20% complete
17.60% complete
18.00% complete
18.40% complete
18.80% complete
19.20% complete
19.60% complete
20.00% complete
20.40% complete
20.80% complete
21.20% complete
21.60% complete
22.00% complete
22.40% complete
22.80% complete
23.20% complete
23.60% complete
24.00% complete
24.40% complete
24.80% complete
25.20% complete
25.60% complete
26.00% complete
26.40% complete
26.80% complete
27.20% complete
27.60% complete
28.00% complete
28.40% complete
28.80% complete
29.20% complete
29.60% complete
30.00% complete
30.40% complete
30.80% complete
31.20% complete
31.60% complete
32.00% complete
32.40% complete
32.80% complete
33.20% complete
33.60% complete
34.00% complete
34.40% complete
34.80% complete
35.20% complete
35.60% complete
36.00% complete
36.40% complete
36.80% complete
37.20% complete
37.60% complete
38.00% complete
38.40% complete
38.80% complete
39.20% complete
39.60% complete
40.00% complete
40.40% complete
40.80% complete
41.20% complete
41.60% complete
42.00% complete
42.40% complete
42.80% complete
43.20% complete
43.60% complete
44.00% complete
44.40% complete
44.80% complete
45.20% complete
45.60% complete
46.00% complete
46.40% complete
46.80% complete
47.20% complete
47.60% complete
48.00% complete
48.40% complete
48.80% complete
49.20% complete
49.60% complete
50.00% complete
50.40% complete
50.80% complete
51.20% complete
51.60% complete
52.00% complete
52.40% complete
52.80% complete
53.20% complete
53.60% complete
54.00% complete
54.40% complete
54.80% complete
55.20% complete
55.60% complete
56.00% complete
56.40% complete
56.80% complete
57.20% complete
57.60% complete
58.00% complete
58.40% complete
58.80% complete
59.20% complete
59.60% complete
60.00% complete
60.40% complete
60.80% complete
61.20% complete
61.60% complete
62.00% complete
62.40% complete
62.80% complete
63.20% complete
63.60% complete
64.00% complete
64.40% complete
64.80% complete
65.20% complete
65.60% complete
66.00% complete
66.40% complete
66.80% complete
67.20% complete
67.60% complete
68.00% complete
68.40% complete
68.80% complete
69.20% complete
69.60% complete
70.00% complete
70.40% complete
70.80% complete
71.20% complete
71.60% complete
72.00% complete
72.40% complete
72.80% complete
73.20% complete
73.60% complete
74.00% complete
74.40% complete
74.80% complete
75.20% complete
75.60% complete
76.00% complete
76.40% complete
76.80% complete
77.20% complete
77.60% complete
78.00% complete
78.40% complete
78.80% complete
79.20% complete
79.60% complete
80.00% complete
80.40% complete
80.80% complete
81.20% complete
81.60% complete
82.00% complete
82.40% complete
82.80% complete
83.20% complete
83.60% complete
84.00% complete
84.40% complete
84.80% complete
85.20% complete
85.60% complete
86.00% complete
86.40% complete
86.80% complete
87.20% complete
87.60% complete
88.00% complete
88.40% complete
88.80% complete
89.20% complete
89.60% complete
90.00% complete
90.40% complete
90.80% complete
91.20% complete
91.60% complete
92.00% complete
92.40% complete
92.80% complete
93.20% complete
93.60% complete
94.00% complete
94.40% complete
94.80% complete
95.20% complete
95.60% complete
96.00% complete
96.40% complete
96.80% complete
97.20% complete
97.60% complete
98.00% complete
98.40% complete
98.80% complete
99.20% complete
99.60% complete
Main took 0:02:34.268042
Total sum of elements (for validation): 1147734
afan@ubuntu:~/python-code\$ python
Python 2.7.1+ (r271:86832, Apr 11 2011, 18:05:24)
[GCC 4.5.2] on linux2
>>> import pstats
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named pstats

ubuntu安装python pstats模块
afan@ubuntu:~\$ sudo apt-get install python-profiler

python-doc

python-profiler

(正在读取数据库 ... 系统当前共安装有 209452 个文件和目录。)

>>> import pstats
>>> p = pstats.Stats('rep.prof')
>>> p.sort_stats('cumulative').print_stats(10)
Thu Jul 28 12:00:35 2011    rep.prof

51927078 function calls (51926888 primitive calls) in 157.339 CPU seconds

Ordered by: cumulative time
List reduced from 535 to 10 due to restriction <10>

ncalls  tottime  percall  cumtime  percall filename:lineno(function)
1    0.037    0.037  157.343  157.343 pure_python.py:1(<module>)
1    0.459    0.459  157.306  157.306 pure_python.py:23(calc_pure_python)
1   94.657   94.657  154.268  154.268 pure_python.py:9(calculate_z_serial_purepython)
51414667   56.833    0.000   56.833    0.000 {abs}
250073    2.778    0.000    2.778    0.000 {range}
1    0.096    0.096    1.986    1.986 /usr/local/lib/python2.7/dist-packages/numpy/__init__.py:106(<module>)
1    0.026    0.026    1.662    1.662 /usr/local/lib/python2.7/dist-packages/numpy/add_newdocs.py:9(<module>)
1    0.075    0.075    1.620    1.620 /usr/local/lib/python2.7/dist-packages/numpy/lib/__init__.py:1(<module>)
1    0.052    0.052    1.029    1.029 /usr/local/lib/python2.7/dist-packages/numpy/lib/type_check.py:3(<module>)
1    0.366    0.366    0.977    0.977 /usr/local/lib/python2.7/dist-packages/numpy/core/__init__.py:2(<module>)

<pstats.Stats instance at 0xb773ef2c>

afan@ubuntu:~/python-code\$ runsnake rep.prof

afan@ubuntu:~/python-code\$ runsnake rep.prof

0

• 评论加载中，请稍候...

发评论

以上网友发言只代表其个人观点，不代表新浪网的观点或立场。

后一篇 >创建Heatmap

新浪BLOG意见反馈留言板　电话：4000520066 提示音后按1键（按当地市话标准计费）　欢迎批评指正

新浪公司 版权所有