title: Python performance of conditional evaluation
body: <p>I was trying to find out if there's any penalty for negating a boolean when evaluation a conditional statement (python 2.6.6). I first tried this simple test (no <code>else</code> branch) </p>
  
  <pre><code>&gt;&gt;&gt; import timeit
  &gt;&gt;&gt; timeit.timeit("if not True: pass", number=100000)
  0.011913061141967773
  &gt;&gt;&gt; timeit.timeit("if True: pass", number=100000)
  0.018882036209106445
  </code></pre>
  
  <p>So I though the results where skewed because the pass statement might be translated to a noop which is at least <em>some</em> operation.</p>
  
  <p>I did a second try and got these results:</p>
  
  <pre><code>&gt;&gt;&gt; timeit.timeit("a=False\nif not a: pass\nelse: pass", number=100000)
  0.02387714385986328
  &gt;&gt;&gt; timeit.timeit("a=False\nif a: pass\nelse: pass", number=100000)
  0.015386819839477539
  &gt;&gt;&gt; timeit.timeit("a=True\nif a: pass\nelse: pass", number=100000)
  0.02389812469482422
  &gt;&gt;&gt; timeit.timeit("a=True\nif not a: pass\nelse: pass", number=100000)
  0.015424966812133789
  </code></pre>
  
  <p>I didn't expect to see any large penalty but from this results it looks like evaluating the <code>else</code> branch is cheaper than the implicit <code>then</code> branch. And the difference is huge!</p>
  
  <p>So The third attempt return these results:</p>
  
  <pre><code>&gt;&gt;&gt; timeit.timeit("if True: a=1\nelse: a=1", number=100000)
  0.022008895874023438
  &gt;&gt;&gt; timeit.timeit("if not True: a=1\nelse: a=1", number=100000)
  0.022121906280517578
  </code></pre>
  
  <p>And finally I got the expected results. Though out of curiosity I tried a last time:</p>
  
  <pre><code>&gt;&gt;&gt; timeit.timeit("if False: a=1\nelse: a=1", number=100000)
  0.02385997772216797
  &gt;&gt;&gt; timeit.timeit("if not False: a=1\nelse: a=1", number=100000)
  0.02244400978088379
  </code></pre>
  
  <p>And that's it...
  I have no idea why the negated condition leading to the <code>then</code> branch is faster.</p>
  
  <p>What might be happening?</p>
  
  <p>All those results are reproducible on my computer, it doesn't matter how many times I run them I get pretty much the same results.</p>
  
  <p>I think the first tests where skewed because the compiler might have removed the <code>else: pass</code> part altogether. Is that possible?</p>
  
  <p>Might all this results be related to the branch predictor in the CPU?</p>
  
  <p>Any other possible culprits?</p>