According to me
Use python's methods (sum()) on python datatypes and use NumPy's methods on NumPy arrays (np.sum()).
massive_array=np.random.random(100000)
massive_array.size
100000
massive_array
array([0.81947279, 0.24254041, 0.76437261, ..., 0.15969415, 0.34502387,
0.15858268])
%timeit sum(massive_array) #Python sum
%timeit np.sum(massive_array) #Numpy sum
16 ms ± 494 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
50.6 µs ± 4.83 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
That's a massive difference!!
What do you guys think???
Top comments (4)
It is to be expected that there's a difference:
I had slightly different timings though.
Given:
these are the timings:
Using Python 3.8
Also Numpy written in C, and executes very quickly as a result. By comparison, Python is a dynamic language that is interpreted by the Python interpreter, converted to byte code, and executed. So compiled C code is always going to be faster. ... Python loops are slower than C loops
Python lists are written in C as well: github.com/python/cpython/blob/mas...
The iterator protocol is in C too: github.com/python/cpython/blob/mas...
The sum function, as most builtins, is written in C as well github.com/python/cpython/blob/c00...
;-)
Also i think an array is a collection of homogeneous data-types which are stored in contagious memory locations, on the other hand, a list in Python is collection of heterogeneous data types stored in non-contagious memory locations.