<h3 dir="auto">Describe your issue.</h3>
<p dir="auto">Ever since version 1.8.0 certain all calls to hypergeom.pmf seem to be slower with certain set of parameters being processed so slow that it is code breaking. For the code shown below, my computer needs ~0.08s to process the loop using scipy 1.7.3 but ~262s on scipy 1.9.3, slowing down the process by more than a factor of 3000.</p>
<h3 dir="auto">Reproducing Code Example</h3>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import numpy as np
import time
from scipy.stats import hypergeom

start = time.time()

for i in range(0, 1000):
    odds = hypergeom.pmf(np.arange(0, 201), 100000, 50000, 200)

end = time.time()
print(&quot;test: &quot; f'{(end - start):0.5}' + &quot;s&quot;)"><pre class="notranslate"><span class="pl-k">import</span> <span class="pl-s1">numpy</span> <span class="pl-k">as</span> <span class="pl-s1">np</span>
<span class="pl-k">import</span> <span class="pl-s1">time</span>
<span class="pl-k">from</span> <span class="pl-s1">scipy</span>.<span class="pl-s1">stats</span> <span class="pl-k">import</span> <span class="pl-s1">hypergeom</span>

<span class="pl-s1">start</span> <span class="pl-c1">=</span> <span class="pl-s1">time</span>.<span class="pl-en">time</span>()

<span class="pl-k">for</span> <span class="pl-s1">i</span> <span class="pl-c1">in</span> <span class="pl-en">range</span>(<span class="pl-c1">0</span>, <span class="pl-c1">1000</span>):
    <span class="pl-s1">odds</span> <span class="pl-c1">=</span> <span class="pl-s1">hypergeom</span>.<span class="pl-en">pmf</span>(<span class="pl-s1">np</span>.<span class="pl-en">arange</span>(<span class="pl-c1">0</span>, <span class="pl-c1">201</span>), <span class="pl-c1">100000</span>, <span class="pl-c1">50000</span>, <span class="pl-c1">200</span>)

<span class="pl-s1">end</span> <span class="pl-c1">=</span> <span class="pl-s1">time</span>.<span class="pl-en">time</span>()
<span class="pl-en">print</span>(<span class="pl-s">"test: "</span> <span class="pl-s">f'<span class="pl-s1"><span class="pl-kos">{</span>(<span class="pl-s1">end</span> <span class="pl-c1">-</span> <span class="pl-s1">start</span>):0.5<span class="pl-kos">}</span></span>'</span> <span class="pl-c1">+</span> <span class="pl-s">"s"</span>)</pre></div>
<h3 dir="auto">Error message</h3>
<div class="highlight highlight-source-shell notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="no error"><pre class="notranslate">no error</pre></div>
<h3 dir="auto">SciPy/NumPy/Python version information</h3>
<p dir="auto">1.7.3 1.22.4 sys.version_info(major=3, minor=10, micro=5, releaselevel='final', serial=0)</p>
<h3 dir="auto">Describe your issue.</h3>
<p dir="auto">While using the <code class="notranslate">fisher_exact</code> test in a loop on some 2x2 tables, I noticed a significant slowdown between Scipy versions 1.7.3 and 1.8.0 (1.8.0 is about 20x slower than 1.7.3). I narrowed it down to the call to <code class="notranslate">hypergeom.cdf</code>, and found a specific set of arguments with which the slowdown can be reproduced (see code example below).</p>
<h3 dir="auto">Reproducing Code Example</h3>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import time
import scipy
from scipy.stats import distributions

ts = time.time()
for _ in range(10000):
    distributions.hypergeom.cdf(0, 48127, 57, 35775)
te = time.time()
print(scipy.__version__, '%.2fs' % (te-ts))

# Output for version info 1.7.3 1.22.2 sys.version_info(major=3, minor=8, micro=10, releaselevel='final', serial=0)
# 1.7.3 1.84s

# Output for version info 1.8.0 1.22.2 sys.version_info(major=3, minor=8, micro=10, releaselevel='final', serial=0)
# 1.8.0 41.11s"><pre class="notranslate"><span class="pl-k">import</span> <span class="pl-s1">time</span>
<span class="pl-k">import</span> <span class="pl-s1">scipy</span>
<span class="pl-k">from</span> <span class="pl-s1">scipy</span>.<span class="pl-s1">stats</span> <span class="pl-k">import</span> <span class="pl-s1">distributions</span>

<span class="pl-s1">ts</span> <span class="pl-c1">=</span> <span class="pl-s1">time</span>.<span class="pl-en">time</span>()
<span class="pl-k">for</span> <span class="pl-s1">_</span> <span class="pl-c1">in</span> <span class="pl-en">range</span>(<span class="pl-c1">10000</span>):
    <span class="pl-s1">distributions</span>.<span class="pl-s1">hypergeom</span>.<span class="pl-en">cdf</span>(<span class="pl-c1">0</span>, <span class="pl-c1">48127</span>, <span class="pl-c1">57</span>, <span class="pl-c1">35775</span>)
<span class="pl-s1">te</span> <span class="pl-c1">=</span> <span class="pl-s1">time</span>.<span class="pl-en">time</span>()
<span class="pl-en">print</span>(<span class="pl-s1">scipy</span>.<span class="pl-s1">__version__</span>, <span class="pl-s">'%.2fs'</span> <span class="pl-c1">%</span> (<span class="pl-s1">te</span><span class="pl-c1">-</span><span class="pl-s1">ts</span>))

<span class="pl-c"># Output for version info 1.7.3 1.22.2 sys.version_info(major=3, minor=8, micro=10, releaselevel='final', serial=0)</span>
<span class="pl-c"># 1.7.3 1.84s</span>

<span class="pl-c"># Output for version info 1.8.0 1.22.2 sys.version_info(major=3, minor=8, micro=10, releaselevel='final', serial=0)</span>
<span class="pl-c"># 1.8.0 41.11s</span></pre></div>
<h3 dir="auto">Error message</h3>
<div class="highlight highlight-source-shell notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="-"><pre class="notranslate">-</pre></div>
<h3 dir="auto">SciPy/NumPy/Python version information</h3>
<p dir="auto">1.8.0 1.22.2 sys.version_info(major=3, minor=8, micro=10, releaselevel='final', serial=0)</p>