Changes between Version 3 and Version 4 of Development/Efficiency


Ignore:
Timestamp:
10/01/13 13:09:14 (5 years ago)
Author:
jdsiiro
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • Development/Efficiency

    v3 v4  
    11= Efficiency of Python Idioms = 
    22 
    3 Profiling and improving the performance of any Python code requires some familiarity with the efficiency of key idioms in the Python language.  Here we document some common idioms and their relative performance. 
     3Profiling and improving the performance of any Python code requires some familiarity with the efficiency of key idioms in the Python language.  As with most environments, there are multiple ways to accomplish the same task.  Here we document some common Python idioms and our preferred form (based on measured performance). 
    44 
    55== Testing Truth == 
     
    88 
    99  - Guidance:  
    10      - ''prefer'' "`if x:`" and "`if not x:`"  
    11      - ''avoid'' "`if x == True:`" or "`if x is True:`" 
     10     - ''prefer'' 
     11     {{{ 
     12#!python 
     13if x: 
     14   pass 
     15# or 
     16if not x: 
     17   pass 
     18}}} 
     19     - ''avoid'' 
     20     {{{ 
     21#!python 
     22if x == True: 
     23   pass 
     24# and 
     25if x is True: 
     26   pass 
     27}}} 
    1228  - Justification: [attachment:test_truth.py source], [attachment:test_truth.txt results] 
    1329 
     
    2036    - ''avoid'' unspecified keyword arguments (e.g., the "`**kwds`" idiom) 
    2137  - Justification: [attachment:test_functions.py source], [attachment:test_functions.txt results] 
     38 
     39== Accessing Dictionaries == 
     40 
     41Working with dictionaries can become tricky when the code depends on if the specified key is already in the dictionary.  The following guidance should hold for types that do not implement a custom implementation of "`__hash__()`". 
     42 
     43 - Guidance: 
     44   - Retrieving a value from a dict that is guaranteed to be present 
     45     - ''prefer'' 
     46     {{{ 
     47#!python 
     48d[x] 
     49}}} 
     50     - ''avoid'' 
     51     {{{ 
     52#!python 
     53d.get(x) 
     54}}} 
     55   - Retrieving a value from a dict if present, otherwise return a fixed value without updating the dictionary 
     56     - ''prefer'' 
     57     {{{ 
     58#!python 
     59d[x] if x in d else None 
     60}}} 
     61     - ''avoid'' 
     62     {{{ 
     63#!python 
     64d.get(x, None) 
     65}}} 
     66   - Setting a value in a dict if the key is not present 
     67     - ''prefer'' 
     68     {{{ 
     69#!python 
     70if x not in d: 
     71   d[x] = v 
     72}}} 
     73     - ''avoid'' 
     74     {{{ 
     75#!python 
     76d.setdefault(x, v) 
     77}}} 
     78   - Returning a value from d, setting a default if the key is not present 
     79     - ''prefer''  
     80     {{{ 
     81#!python 
     82if x in d: 
     83   a = d[x] 
     84else: 
     85   a = v 
     86   d[x] = a 
     87}}} 
     88     - ''avoid'' 
     89     {{{ 
     90#!python 
     91a = d.setdefault(x, v) 
     92}}} 
     93   - (Iteratively) constructing a dictionary 
     94     - ''prefer'' passing a tuple-generator directly to the dict constructor, e.g.: 
     95     {{{ 
     96#!python 
     97ans = dict( d.iteritems() ) 
     98}}} 
     99     - ''fallback'' to setting the elements one at a time, e.g.: 
     100     {{{ 
     101#!python 
     102ans = {} 
     103for i,v in d.iteritems(): 
     104   ans[i] = v 
     105}}} 
     106     - ''avoid'' using a generator that returns a tuple, e.g.: 
     107     {{{ 
     108#!python 
     109ans = dict((i,v) for i,v in d.iteritems()) 
     110}}} 
     111  - Justification: [attachment:test_dict.py source], [attachment:test_dict.txt results]