title: Python's sum vs. NumPy's numpy.sum
body: <p>What are the differences in performance and behavior between using Python's native <code>sum</code> function and NumPy's <code>numpy.sum</code>? <code>sum</code> works on NumPy's arrays and <code>numpy.sum</code> works on Python lists and they both return the same effective result (haven't tested edge cases such as overflow) but different types.</p>
  
  <pre><code>&gt;&gt;&gt; import numpy as np
  &gt;&gt;&gt; np_a = np.array(range(5))
  &gt;&gt;&gt; np_a
  array([0, 1, 2, 3, 4])
  &gt;&gt;&gt; type(np_a)
  &lt;class 'numpy.ndarray')
  
  &gt;&gt;&gt; py_a = list(range(5))
  &gt;&gt;&gt; py_a
  [0, 1, 2, 3, 4]
  &gt;&gt;&gt; type(py_a)
  &lt;class 'list'&gt;
  
  # The numerical answer (10) is the same for the following sums:
  &gt;&gt;&gt; type(np.sum(np_a))
  &lt;class 'numpy.int32'&gt;
  &gt;&gt;&gt; type(sum(np_a))
  &lt;class 'numpy.int32'&gt;
  &gt;&gt;&gt; type(np.sum(py_a))
  &lt;class 'numpy.int32'&gt;
  &gt;&gt;&gt; type(sum(py_a))
  &lt;class 'int'&gt;
  </code></pre>
  
  <p><strong>Edit:</strong> I think my practical question here is would using <code>numpy.sum</code> on a list of Python integers be any faster than using Python's own <code>sum</code>?</p>
  
  <p>Additionally, what are the implications (including performance) of using a Python integer versus a scalar <code>numpy.int32</code>? For example, for <code>a += 1</code>, is there a behavior or performance difference if the type of <code>a</code> is a Python integer or a <code>numpy.int32</code>? I am curious if it is faster to use a NumPy scalar datatype such as <code>numpy.int32</code> for a value that is added or subtracted a lot in Python code.</p>
  
  <p>For clarification, I am working on a bioinformatics simulation which partly consists of collapsing multidimensional <code>numpy.ndarray</code>s into single scalar sums which are then additionally processed. I am using Python 3.2 and NumPy 1.6.</p>
  
  <p>Thanks in advance!</p>