title: Python 3 type hints for performance optimizations
body: <p>PEP 484 says &quot;Using type hints for performance optimizations is left as an exercise for the reader.&quot; This suggests to me that, like Common Lisp, type declarations can be used to set aside type dispatch inside performance-intensive functions when I swear I know what I'm doing. To try this out for myself, I whipped up a little benchmark to calculate pi using a p-series. First I do it the naive way, then I try to be clever and exploit the type hints for performance:</p>
  <pre><code>import math
  import time
  
  def baselpi0(n):
      baselsum = 0;
      for i in range(1,n):
          baselsum += 1.0 / (i * i)
      return math.sqrt(6.0 * baselsum)
  
  def baselpi1(n : int) -&gt; float:
      n = float(n)
      baselsum  = 0.0
      i = 1.0
      while i &lt; n:
          baselsum += 1.0 / (i * i)
          i += 1.0
      return math.sqrt(6.0 * baselsum)
  
  start = time.time()
  print(baselpi0(1000000000))
  end = time.time()
  print(end - start)
  start = time.time()
  print(baselpi1(1000000000))
  end = time.time()
  print(end - start)
  </code></pre>
  <p>The Common Lisp analogy I'm trying to emulate is:</p>
  <pre class="lang-lisp prettyprint-override"><code>(defun baselpi0 (n)
    (let ((baselsum 0.0d0))
      (loop for i from 1 to n do
        (setf baselsum (+ baselsum (/ 1.0 (* i i)))))
      (sqrt (* 6 baselsum))))
  
  (defun baselpi1 (n)
    (let ((baselsum 0.0d0)
          (n (coerce n 'double-float)))
      (declare (type double-float baselsum n)
           (optimize (speed 3) (safety 0) (debug 0)))
      (loop for i from 1.0d0 to n do
            (setf baselsum (+ baselsum (/ 1.0d0 (* i i)))))
      (sqrt (* 6.0d0 baselsum))))
  
  (time (princ (baselpi0 1000000000)))
  (time (princ (baselpi1 1000000000)))
  (exit)
  </code></pre>
  <p>On my machine, the lisp version run with sbcl takes 22 seconds for the slow version and 4 seconds for the type-hinted version, same as C. CPython takes 162 seconds for the naive version and 141 for the type-hinted version. Pypy runs the non-type-hinted version in just under 5 seconds, but the library support isn't good enough for my project.</p>
  <p>Is there a way I can improve my type hinted version to get performance closer to lisp or Pypy?</p>