Size: 477
Comment:
|
Size: 3235
Comment:
|
Deletions are marked like this. | Additions are marked like this. |
Line 10: | Line 10: |
= String Concatenation = | |
Line 11: | Line 12: |
(1) | |
Line 12: | Line 14: |
= 文章大标 = ''简述'' == 章标题1 == |
Normal Code: |
Line 16: | Line 16: |
=== 小节标题1 === {{{ #!python Python code |
{{{#!python s = "" for substring in list: s += substring |
Line 22: | Line 22: |
==== 次节标题1 ==== xxx |
Optimized Code: |
Line 25: | Line 24: |
== 章标题2 == === 小节标题2 === {{{ 其它 代码引用 |
{{{#!python s = "".join(list) |
Line 33: | Line 28: |
==== 次节标题2 ==== yyy |
(2) |
Line 36: | Line 30: |
Normal Code: {{{#!python s = "" for x in list: s += someFunction(x) }}} Optimized Code: {{{#!python slist = [someFunction(x) for x in somelist] s = "".join(slist) }}} (3) Normal Code: {{{#!python out = "<html>" + head + prologue + query + tail + "</html>" }}} Optimized Code: {{{#!python out = "<html>%(head)s%(prologue)s%(query)s%(tail)s</html>" % locals() }}} == Loops == (1)Converting to upper case: Normal Code: {{{#!python newlist = [] for word in oldlist: newlist.append(word.upper()) }}} Optimized Code: {{{#!python #(map() is fast but will be removed from Py3000) newlist = map(str.upper, oldlist) #Or(List comprehensions, Py > 2.0) newlist = [s.upper() for s in oldlist] #Or(Generator expressions, Py > 2.4) newlist = (s.upper() for s in oldlist) }}} == OOP == (1)Suppose you cannot use map() or list comprehension, just remember Avoiding dots: {{{#!python upper = str.upper newlist = [] append = newlist.append # loop without dots for word in list: append(upper(word)) }}} == Local Variables == (1)Final speedup method is to use local instead of global vars. {{{#!python def func(): upper = str.upper newlist = [] append = newlist.append for word in words: append(upper(word)) return newlist }}} == Dictionary == (1)Avoid if in for loops: Normal Code: {{{#!python wdict= {} for word in words: if word not in wdict: wdict[word] = 0 wdict[word] += 1 }}} Optimized Code: {{{#!python #(Py < 2.x) wdict = {} for word in words: try: wdict[word] += 1 except KeyError: wdict[word] = 1 #(Py > 2.x) wdict = {} get = wdict.get for word in words: wdict[word] = get(word, 0) + 1 }}} Also , if the value stored in the dict is an object or a list, you could also use the dict.setdefault method, e.g. {{{#!python wdict.setdefault(key, []).append(newElement) }}} This avoids having to lookup the twice. == Import == (1)import inside the function is more efficiently. (2)Do import once, {{{#!python #check pack = None def parse_pack(): global pack if pack is None: import pack ... }}} == Data Aggregation == (1)Avoiding function call in for loop Normal Code: {{{#!python import time x = 0 def doit(i): global x x = x + 1 list = range(100000) t = time.time() for i in list: doit(i) print "%.3f" %(time.time() -t ) }}} Optimized Code: {{{#!python import time x = 0 def doit(i): global x for i in list: x = x + 1 x = x + 1 list = range(100000) t = time.time() doit(list) print "%.3f" %(time.time() -t ) }}} (What?? about 4 times faster!! ) == range() -> xrange() == It is implemented in Pure C. |
Python性能调试笔记
::-- Roka [DateTime(2007-04-26T14:46:55Z)] TableOfContents
1. String Concatenation
(1)
Normal Code:
Optimized Code:
1 s = "".join(list)
(2)
Normal Code:
Optimized Code:
(3)
Normal Code:
1 out = "<html>" + head + prologue + query + tail + "</html>"
Optimized Code:
1 out = "<html>%(head)s%(prologue)s%(query)s%(tail)s</html>" % locals()
1.1. Loops
(1)Converting to upper case:
Normal Code:
Optimized Code:
1.2. OOP
(1)Suppose you cannot use map() or list comprehension, just remember Avoiding dots:
1.3. Local Variables
(1)Final speedup method is to use local instead of global vars.
1.4. Dictionary
(1)Avoid if in for loops:
Normal Code:
Optimized Code:
Also , if the value stored in the dict is an object or a list, you could also use the dict.setdefault method, e.g.
1 wdict.setdefault(key, []).append(newElement)
This avoids having to lookup the twice.
1.5. Import
(1)import inside the function is more efficiently.
(2)Do import once,
1.6. Data Aggregation
(1)Avoiding function call in for loop
Normal Code:
Optimized Code:
(What?? about 4 times faster!! )
1.7. range() -> xrange()
It is implemented in Pure C.