title: Optimization in Python - do's, don'ts and rules of thumb
body: <p>Well I was reading this <a href="http://handyfloss.wordpress.com/2008/02/17/summary-of-my-python-optimization-adventures/" rel="noreferrer">post</a> and then I came across a code which was:</p>
  
  <pre><code>jokes=range(1000000)
  domain=[(0,(len(jokes)*2)-i-1) for i in range(0,len(jokes)*2)]
  </code></pre>
  
  <p>I thought wouldn't it be better to calculate the value of len(jokes) once outside the list comprehension?</p>
  
  <p>Well I tried it and timed three codes</p>
  
  <pre><code>jv@Pioneer:~$ python -m timeit -s 'jokes=range(1000000);domain=[(0,(len(jokes)*2)-i-1) for i in range(0,len(jokes)*2)]'
  10000000 loops, best of 3: 0.0352 usec per loop
  jv@Pioneer:~$ python -m timeit -s 'jokes=range(1000000);l=len(jokes);domain=[(0,(l*2)-i-1) for i in range(0,l*2)]'
  10000000 loops, best of 3: 0.0343 usec per loop
  jv@Pioneer:~$ python -m timeit -s 'jokes=range(1000000);l=len(jokes)*2;domain=[(0,l-i-1) for i in range(0,l)]'
  10000000 loops, best of 3: 0.0333 usec per loop
  </code></pre>
  
  <p>Observing the marginal difference 2.55% between the first and the second made me think - is the first list comprehension </p>
  
  <pre><code>domain=[(0,(len(jokes)*2)-i-1) for i in range(0,len(jokes)*2)]
  </code></pre>
  
  <p>optimized internally by python? or is 2.55% a big enough optimization (given that the len(jokes)=1000000)?</p>
  
  <p>If this is - What are the other implicit/internal optimizations in Python ?</p>
  
  <p>What are the <code>developer's rules of thumb for optimization in Python</code>?</p>
  
  <p><strong>Edit1</strong>: Since most of the answers are "don't optimize, do it later if its slow" and I got some tips and links from <code>Triptych</code> and <code>Ali A</code> for the <strong>do's</strong>.
  I will change the question a bit and request for <strong>don'ts</strong>. </p>
  
  <p>Can we have some experiences from people who faced the '<strong>slowness</strong>', what was the problem and how it was corrected? </p>
  
  <p><strong>Edit2</strong>: For those who haven't here is an <a href="http://www.python.org/doc/essays/list2str/" rel="noreferrer">interesting read</a></p>
  
  <p><strong>Edit3:</strong> Incorrect usage of <code>timeit</code> in question please see <strong>dF's</strong> answer for correct usage and hence timings for the three codes.</p>