title: calculating the number of k-combinations with and without SciPy
body: <p>I'm puzzled by the fact that the function <a href="http://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.misc.comb.html" rel="noreferrer"><code>comb</code> of SciPy</a> appears to be slower than a naive Python implementation. This is the measured time for two equivalent programs solving the <a href="https://projecteuler.net/problem=53" rel="noreferrer">Problem 53 of Project Euler</a>.</p>
  
  <hr>
  
  <h3>With SciPy:</h3>
  
  <pre><code>%%timeit
  from scipy.misc import comb
  
  result = 0
  for n in range(1, 101):
      for k in range(1, n + 1):
          if comb(n, k) &gt; 1000000:
              result += 1
  result
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>1 loops, best of 3: 483 ms per loop
  </code></pre>
  
  <hr>
  
  <h3>Without SciPy:</h3>
  
  <pre><code>%%timeit
  from math import factorial
  
  def comb(n, k):
      return factorial(n) / factorial(k) / factorial(n - k)
  
  result = 0
  for n in range(1, 101):
      for k in range(1, n + 1):
          if comb(n, k) &gt; 1000000:
              result += 1
  result
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>10 loops, best of 3: 86.9 ms per loop
  </code></pre>
  
  <hr>
  
  <p>The second version is about 5 times faster (tested on two Macs, python-2.7.9-1, IPython 2.3.1-py27_0). Is this an expected behaviour of the <code>comb</code> function of SciPy (<a href="https://github.com/scipy/scipy/blob/v0.14.0/scipy/special/basic.py#L1144" rel="noreferrer">source code</a>)? What am I doing wrong?</p>
  
  <hr>
  
  <p>Edit (SciPy from the Anaconda 3.7.3-py27_0 distribution):</p>
  
  <pre><code>import scipy; print scipy.version.version
  0.12.0
  </code></pre>
  
  <hr>
  
  <p>Edit (same difference outside IPython):</p>
  
  <pre><code>$ time python with_scipy.py
  real    0m0.700s
  user    0m0.610s
  sys     0m0.069s
  
  $ time python without_scipy.py
  real    0m0.134s
  user    0m0.099s
  sys     0m0.015s
  </code></pre>