Size: 3237
Comment:
|
Size: 3275
Comment:
|
Deletions are marked like this. | Additions are marked like this. |
Line 10: | Line 10: |
== String Concatenation == | = 概要 = TODO == 字符串连接 == |
Line 14: | Line 19: |
Normal Code: | 普通代码: |
Line 22: | Line 27: |
Optimized Code: | 高性能代码: |
Line 30: | Line 35: |
Normal Code: | 普通代码: |
Line 38: | Line 43: |
Optimized Code: | 高性能代码: |
Line 46: | Line 52: |
Normal Code: | 普通代码: |
Line 51: | Line 58: |
Optimized Code: | 高性能代码: |
Line 56: | Line 64: |
== Loops == (1)Converting to upper case: Normal Code: |
== 循环 == (1)一个转换大写的例程: 普通代码: |
Line 67: | Line 76: |
Optimized Code: {{{#!python #(map() is fast but will be removed from Py3000) |
高性能代码: {{{#!python #(map()函数是C语言实现,性能比较高,但是会在Py3000消失) |
Line 77: | Line 87: |
== OOP == (1)Suppose you cannot use map() or list comprehension, just remember Avoiding dots: |
== 面向对象 == (1)假设不能使用map()和list comprehension,你只能使用循环时要避免”带点循环“: |
Line 89: | Line 99: |
== Local Variables == (1)Final speedup method is to use local instead of global vars. |
== 本地变量 == (1)终极办法-使用本地变量代替全局变量 |
Line 102: | Line 112: |
== Dictionary == (1)Avoid if in for loops: Normal Code: |
== 字典 == (1)不要带IF循环: 普通代码: |
Line 115: | Line 125: |
Optimized Code: | 高性能代码: |
Line 132: | Line 142: |
Also , if the value stored in the dict is an object or a list, you could also use the dict.setdefault method, e.g. | 如果在字典里的是对象或列表,你还可以用dict.setdefault 方法 |
Line 137: | Line 147: |
This avoids having to lookup the twice. | |
Line 141: | Line 150: |
(1)import inside the function is more efficiently. (2)Do import once, |
(1)在本地import会比全局import高效。 (2)保证只import一次。 |
Line 155: | Line 164: |
== Data Aggregation == (1)Avoiding function call in for loop Normal Code: |
== 数据集合处理 == (1)避免在循环中进行函数调用 普通代码: |
Line 175: | Line 184: |
Optimized Code: | 高性能代码: |
Line 192: | Line 201: |
(What?? about 4 times faster!! ) == range() -> xrange() == It is implemented in Pure C. |
(什么??竟然快了4倍以上!!) == 使用xrange()代替range() == 毕竟xrange()是C完全实现的。 |
Python性能调试笔记
::-- Roka [DateTime(2007-04-26T14:46:55Z)] TableOfContents
1. 概要
TODO
1.1. 字符串连接
(1)
普通代码:
高性能代码:
1 s = "".join(list)
(2)
普通代码:
高性能代码:
(3)
普通代码:
1 out = "<html>" + head + prologue + query + tail + "</html>"
高性能代码:
1 out = "<html>%(head)s%(prologue)s%(query)s%(tail)s</html>" % locals()
1.2. 循环
(1)一个转换大写的例程:
普通代码:
高性能代码:
1.3. 面向对象
(1)假设不能使用map()和list comprehension,你只能使用循环时要避免”带点循环“:
1.4. 本地变量
(1)终极办法-使用本地变量代替全局变量
1.5. 字典
(1)不要带IF循环:
普通代码:
高性能代码:
如果在字典里的是对象或列表,你还可以用dict.setdefault 方法
1 wdict.setdefault(key, []).append(newElement)
1.6. Import
(1)在本地import会比全局import高效。
(2)保证只import一次。
1.7. 数据集合处理
(1)避免在循环中进行函数调用
普通代码:
高性能代码:
(什么??竟然快了4倍以上!!)
1.8. 使用xrange()代替range()
毕竟xrange()是C完全实现的。