title: Why does using arguments make this function so much slower?
body: <p>I have recently been working on creating a prime-number finding program. However, <strong>I came to notice that one function was much slower when it used arguments than when it used pre-set values</strong>.</p>
  
  <p>Across 3 different versions, it becomes clear that the variables are significantly slowing down the program, and I would like to know why.</p>
  
  <h1><em>Version 1</em>: 7.5 seconds</h1>
  
  <p>Here was the original (somewhat simplified for this question) function:</p>
  
  <pre><code>def version1(n, p):
    return ((n*n - 2) &amp; ((1 &lt;&lt; p) - 1)) + ((n*n - 2) &gt;&gt; p)
  </code></pre>
  
  <p>When ran with the <code>timeit</code> module 100 times:</p>
  
  <pre><code>timeit.timeit("version1(200, 500000000)", "from __main__ import version1", number=100)
  </code></pre>
  
  <p>it takes <code>7.5</code> seconds.</p>
  
  <h1><em>Version 2</em>: 0.0001 seconds</h1>
  
  <p>However, here is the second version, in which there are no parameters, and the numbers are directly placed in the return value. The equasion is exactly the same as <em>Version 1</em>:</p>
  
  <pre><code>def version2():
    return ((200*200 - 2) &amp; ((1 &lt;&lt; 500000000) - 1)) + ((200*200 - 2) &gt;&gt; 500000000)
  </code></pre>
  
  <p>When ran with the <code>timeit</code> module 100 times:</p>
  
  <pre><code>timeit.timeit("version2()", "from __main__ import version2", number=100
  </code></pre>
  
  <p>in this only takes <code>0.00001</code> seconds!</p>
  
  <h1><em>Version 3</em>: 6.3 seconds</h1>
  
  <p>Lastly, for completeness, I tried a version that had no parameters, but still kept its values as variables:</p>
  
  <pre><code>def version3():
    n = 200
    p = 500000000
    return ((n*n - 2) &amp; ((1 &lt;&lt; p) - 1)) + ((n*n - 2) &gt;&gt; p)
  </code></pre>
  
  <p>When ran with <code>timeit</code>:</p>
  
  <pre><code>timeit.timeit("version3()", "from __main__ import version3", number = 100)
  </code></pre>
  
  <p>it took <code>6.3</code> seconds, which is relatively close to <em>Version 1</em>.</p>
  
  <p><strong>Why is it that the same function can take so much longer when there are variables involved, and how can I make <em>Version 1</em> more effient?</strong></p>