Changes between Version 53 and Version 54 of DeveloperGuidelines/Optimisation


Ignore:
Timestamp:
08/29/10 08:32:10 (11 years ago)
Author:
Fran Boon
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • DeveloperGuidelines/Optimisation

    v53 v54  
    5353
    5454NB If only expecting one record then the limitby provides a big speedup!
    55 
     55----
     56{{{
     57dict.get("x")
     58}}}
     59is significantly slower than
     60{{{
     61dict["x"]
     62}}}
     63
     64but...the construction:
     65{{{
     66y = dict.get("x", None)
     67   if y:
     68}}}
     69is as fast as
     70{{{
     71if x in dict:
     72   y = dict["x"]
     73}}}
     74
     75Also, keys() is slowing things down.
     76{{{
     77if a in dict.keys()
     78}}}
     79is ~25% slower than:
     80{{{
     81if a in dict
     82}}}
     83
     84Another thing is that the profiler showed that there is extensive use of
     85isinstance. So I tried to find an alternative, which would be:
     86{{{
     87if type(x) == "yyy"
     88}}}
     89In fact, this is ~30% faster than isinstance, but it won't find subclasses.
     90So, if you test for:
     91{{{
     92if isinstance(x, dict)
     93}}}
     94and want Storages to match, then you cannot replace isinstance.
     95
     96A real killer is hasattr().
     97I ran 5 million loops of
     98{{{
     99if "a" in dict:
     100}}}
     101vs.
     102{{{
     103if hasattr(dict, "a")
     104}}}
     105which was 4.5s vs. 12s.
     106
     107Hence - for dicts, avoid hasattr to test for containment.
     108----
    56109Javascript string concatenation:
    57110 * http://aymanh.com/9-javascript-tips-you-may-not-know#String_Concatenation_vs._Array.join