title: Slow abs() function in Python. Explain?
body: <p>So I was bored, and I decided to come up with a method to calculate pi. I implemented it, and it ran well. I wanted to optimize it, so I ran the profiler. It took about 26 seconds. I discovered that the <code>abs()</code> function took up a lot of lag, so I came up with a way to avoid the <code>abs()</code> function. After that, I could run it in 8 seconds! Can someone explain to me why the <code>abs()</code> function was taking so long?</p>
  
  <p>Here is the code without <code>abs()</code>:</p>
  
  <pre><code>def picalc(radius = 10000000):
      total = 0
      x = 0
      y = radius
      for i in range(radius + 1):
          x1 = i
          y1 = (radius ** 2 - x1 ** 2) ** 0.5
          total += ((x1 - x) ** 2 + (y1 - y) ** 2) ** 0.5
          x = x1
          y = y1
      print(total / (radius / 2))
  import profile
  profile.run('picalc()')
  </code></pre>
  
  <p>If I change the line <code>total += ((x1 - x) ** 2 + (y1 - y) ** 2) ** 0.5</code> to <code>total += (abs(x1 - x) ** 2 + abs(y1 - y) ** 2) ** 0.5</code>, the operation runs MUCH slower.</p>
  
  <p>EDIT: I know that the negatives cancel when squaring. That was a mistake I made.</p>
  
  <p>EDIT x2: I tried substituting <code>total += ((x1 - x) ** 2 + (y1 - y) ** 2) ** 0.5</code> with <code>total += math.hypot(x1 - x, y1 - y)</code>, but the profiler tells me it took 10 seconds longer! I read the docs and they said that the math library contains thin wrappers to the C math library (at least in IDLE). How can C be slower than Python in this case?</p>