title: Why is NumPy sometimes slower than NumPy + plain Python loop?
body: <p>This is based on <a href="https://stackoverflow.com/questions/52772883/efficient-implementation-of-the-following-code-snippet">this question</a> asked 2018-10.</p>
  
  <p>Consider the following code. Three simple functions to count non-zero elements in a NumPy 3D array (1000 × 1000 × 1000).</p>
  
  <pre><code>import numpy as np
  
  def f_1(arr):
      return np.sum(arr &gt; 0)
  
  def f_2(arr):
      ans = 0
      for val in range(arr.shape[0]):
          ans += np.sum(arr[val, :, :] &gt; 0)
      return ans
  
  def f_3(arr):
      return np.count_nonzero(arr)
  
  if __name__ == '__main__':
  
      data = np.random.randint(0, 10, (1_000, 1_000, 1_000))
      print(f_1(data))
      print(f_2(data))
      print(f_3(data))
  </code></pre>
  
  <p>Runtimes on my machine (Python 3.7.?, Windows&nbsp;10, NumPy 1.16.?):</p>
  
  <pre><code>%timeit f_1(data)
  1.73 s ± 21.7 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
  
  %timeit f_2(data)
  1.4 s ± 1.36 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
  
  %timeit f_3(data)
  2.38 s ± 956 µs per loop (mean ± std. dev. of 7 runs, 1 loop each)
  </code></pre>
  
  <p>So, <code>f_2()</code> works faster than <code>f_1()</code> and <code>f_3()</code>. However, it's not the case with <code>data</code> of smaller size. The question is - why so? Is it NumPy, Python, or something else?</p>