title: Why is if True slower than if 1?
body: <p>Why is <code>if True</code> slower than <code>if 1</code> in Python? Shouldn't <code>if True</code> be faster than <code>if 1</code>?</p>
  
  <p>I was trying to learn the <code>timeit</code> module. Starting with the basics, I tried these:</p>
  
  <pre><code>&gt;&gt;&gt; def test1():
  ...     if True:
  ...         return 1
  ...     else:
  ...         return 0
  
  &gt;&gt;&gt; print timeit("test1()", setup = "from __main__ import test1")
  0.193144083023
  
  
  &gt;&gt;&gt; def test2():
  ...     if 1:
  ...         return 1
  ...     else:
  ...         return 0
  
  &gt;&gt;&gt; print timeit("test2()", setup = "from __main__ import test2")
  0.162086009979
  
  
  &gt;&gt;&gt; def test3():
  ...     if True:
  ...             return True
  ...     else:
  ...             return False
  
  &gt;&gt;&gt; print timeit("test3()", setup = "from __main__ import test3")
  0.214574098587
  
  &gt;&gt;&gt; def test4():
  ...     if 1:
  ...             return True
  ...     else:
  ...             return False
  
  &gt;&gt;&gt; print timeit("test4()", setup = "from __main__ import test4")
  0.160849094391
  </code></pre>
  
  <p>I am confused by these things:</p>
  
  <ol>
  <li>According to the response from Mr. Sylvain Defresne in <a href="https://stackoverflow.com/questions/100732/why-is-if-not-someobj-better-than-if-someobj-none-in-python">this question</a>, everything is implicitly converted to a <code>bool</code> first and then checked. So why is <code>if True</code> slower than <code>if 1</code>?</li>
  <li>Why is <code>test3</code> slower than <code>test1</code> even though only the <code>return</code> values are different?</li>
  <li>Like Question 2, but why is <code>test4</code> a little <em>faster</em> than <code>test2</code>?</li>
  </ol>
  
  <p>NOTE: I ran <code>timeit</code> three times and took the average of the results, then posted the times here along with the code.</p>
  
  <p>This question does not relate to how to do micro benchmarking(which I did in this example but I also understand that it is too basic) but why checking a 'True' variable is slower than a constant.</p>