title: Why is an or statement faster than an in statement?
body: <p>In my project, I had to check whether a value was either one of two values. Since I could approach this with an <code>if or</code> statement or an <code>if in</code> statement, and since I did not know which one of the two would run faster, I ran the following code to check their respective performances:</p>
  
  <pre><code>import time
  import datetime
  from scipy.stats import ttest_ind
  
  
  def if_in(t):
      bln = False
      for z in range(400):
          if z % 100 == 0: print("test1", z)
          starttime = time.time()
          for x in range(1000000):
              for i in range(5):
                  if i in [2, 4]:
                      bln = True
          t.append(time.time() - starttime)
      return t
  
  
  def if_or(t):
      bln = False
      for z in range(400):
          if z % 100 == 0: print("test2", z)
          starttime = time.time()
          for x in range(1000000):
              for i in range(5):
                  if i == 2 or i == 4:
                      bln = True
          t.append(time.time() - starttime)
      return t
  
  
  st = time.time()
  
  times1 = if_in([])
  times2 = if_or([])
  
  t, p = ttest_ind(times1, times2)
  
  print("\nTotal execution time:", str(datetime.timedelta(seconds=time.time() - st)))
  t1mean = sum(times1) / len(times1)
  t2mean = sum(times2) / len(times2)
  print("Test1 mean:", t1mean, "\nTest2 mean:", t2mean)
  print("\nT-test p-score:", p)
  </code></pre>
  
  <p>Which printed:</p>
  
  <pre class="lang-none prettyprint-override"><code>Test1 mean: 0.47915725767612455 
  Test2 mean: 0.46851890563964843
  
  T-test p-score: 0.001033983121482868
  </code></pre>
  
  <p>The p-value indicates the difference between execution time of the <code>in</code> and <code>or</code> statement loop to be significant. </p>
  
  <p>Why is this difference? For the 'or' method, I would assume further checking would stop when the first condition is deemed to be true. The same, I again assume, is true for the 'in' method. However, one does run faster than the other.</p>
  
  <p>Additionally, would this hold up for more conditions? E.g. when <code>i</code> should be checked to be one of 100 values?</p>