Numpy Sum Performance
<p>I made a plot of the time required for numpy.sum() to sum elements of an array. Here is my plot:</p>

<p><a href="http://i.stack.imgur.com/MSSpd.png" rel="nofollow"><img src="http://i.stack.imgur.com/MSSpd.png" alt="Numpy sum performance"></a></p>

<p>The x axis denotes the number of entries that have been summed. 
The y axis denotes the time used to sum those entries (in seconds).</p>

<p>For the entries, I used an array of 10<sup>7</sup> random values. The same array is used every time.</p>

<p>I can't understand why the time used by the computer to sum 10<sup>1</sup> entries is larger than the time required for 10<sup>2</sup> entries. Why is it that the first part of the curve is decreasing?</p>

<p><b> EDIT </b>
Here is the code used for measuring the performance:</p>

<pre><code>def numpy_sum(vector, n):
    new_vector = vector[:n]

    start_time = time.perf_counter()
    np.sum(new_vector)

    return time.perf_counter() - start_time
</code></pre>

<p>When you measure performance of some code, it is advisable to run it multiple times and take the average to avoid skewed results.</p>

<p>By default, timeit module execute the code for 1 million times. I am not seeing the issue you mentioned even if I run it only for 100 times. Here is an equivalent version of your code using timeit module:</p>

<pre><code>from timeit import Timer
import numpy as np
import random
#generate random list
random_list =[random.randint(1,100) for x in range(1000000)]
np_sum_time= Timer("np.sum(new_list)", "from __main__ import new_list,np")
for n in (10**x for x in range(1,7)):
    new_list = random_list[:n]
    print "{0} Entries Sum Time: {1} Seconds".format(n,np_sum_time.timeit(number=100))
</code></pre>

<p>And the results:</p>

<pre><code>10 Entries Sum Time: 0.000567059064269 Seconds
100 Entries Sum Time: 0.00100197540542 Seconds
1000 Entries Sum Time: 0.00490724190233 Seconds
10000 Entries Sum Time: 0.0441321414402 Seconds
100000 Entries Sum Time: 0.430979001174 Seconds
1000000 Entries Sum Time: 4.46029030987 Seconds
</code></pre>

python performance numpy sum