Here is the time using np.dot:
import numpy as np
import timeit
x = np.random.random(size=10**7)
a = np.ones(x.size)
%time np.dot(x, a)
Wall time: 11 ms
5001679.267011214
Here is the time using for-loops:
import numpy as np
import timeit
x = np.random.random(size=10**7)
a = np.ones(x.size)
def innfeldi(vigur1, vigur2):
return sum([vigu1[i]*vigur2[i] for i in range(len(vigur1))])
%timeit innfeldi(x, a)
Wall time: 4.78 s
4998161.0032265792
Because np.dot
executes the actual arithmetic operations and the enclosing loop in compiled code, which is much faster than the Python interpreter.
This principle, grouping repetitive things together and cutting out the interpreter as much as possible is why we can write numerical code in high-level languages such as Python or matlab that runs at acceptable speed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With