This surprises me a bit. I've been testing performances.
In [1]: import numpy as np
In [2]: %timeit a = np.sum(range(100000))
Out[2]: 100 loops, best of 3: 16.7 ms per loop
In [3]: %timeit a = np.sum([range(100000)])
Out[3]: 100 loops, best of 3: 16.7 ms per loop
In [4]: %timeit a = np.sum([i for i in range(100000)])
Out[4]: 100 loops, best of 3: 12 ms per loop
In [5]: %timeit a = np.sum((i for i in range(100000)))
Out[5]: 100 loops, best of 3: 8.43 ms per loop
I'm trying to understand the inner working as well as learn how to generalize to have a best practice. Why is 4 (building a new generator) is better than 1?
I understand why creating a list takes more time. But again, why 3 is better than 2? And why isn't 2 worse than 1? Is a list being built at 1?
I'm using a from numpy import *
.