title: Speed of calculating powers (in python)
body: <p>I'm curious as to why it's so much faster to multiply than to take powers in python (though from what I've read this may well be true in many other languages too). For example it's much faster to do</p>
  
  <pre><code>x*x
  </code></pre>
  
  <p>than</p>
  
  <pre><code>x**2
  </code></pre>
  
  <p>I suppose the ** operator is more general and can also deal with fractional powers. But if that's why it's so much slower, why doesn't it perform a check for an int exponent and then just do the multiplication?
  <br/></p>
  
  <p><strong>Edit:</strong> Here's some example code I tried...</p>
  
  <pre><code>def pow1(r, n):
    for i in range(r):
      p = i**n
  
  def pow2(r, n):
    for i in range(r):
      p = 1
      for j in range(n):
        p *= i
  </code></pre>
  
  <p>Now, pow2 is just a quick example and is clearly not optimised!<br/>
  But even so I find that using n = 2 and r = 1,000,000, then pow1 takes ~ 2500ms and pow2 takes ~ 1700ms.<br/>
   I admit that for large values of n, then pow1 does get much quicker than pow2. But that's not too surprising.<br/></p>