The vectorized version of a code is comparably fast on both Python and Matlab. But, sometimes I have to use (for) loops. In these cases, Python loops are really slow. Why is it this way?
In codes below, it is clear that the vectorized versions are running similarly. But the for loop version in Matlab is comparably good, while the Python version is really slow.
Python Code
import numpy as np
import time
N = 10000000
a = np.random.random(N)
b = np.random.random(N)
#vectorized
start_time = time.time()
c = np.dot(a,b)
print(c)
print("--- %s msec ---" % (time.time() - start_time))
#non-vectorized
start_time = time.time()
c = 0
for ii in range(N):
c = c + a[ii]*b[ii]
print(c)
print("--- %s msec ---" % (time.time() - start_time))
Matlab Code
N = 10000000;
a = rand(N, 1);
b = rand(N, 1);
tic
c = dot(a, b);
disp(c);
toc
tic
c = 0;
for ii = 1:N
c = c + a(ii) * b(ii);
end
disp(c)
toc
Python output:
2500596.6897313246
--- 0.008107662200927734 sec ---
2500596.689731018
--- 3.6871590614318848 sec ---
Matlab output:
2.4997e+06
Elapsed time is 0.027503 seconds.
2.4997+06
Elapsed time is 0.032014 seconds.
While the Python vectorized version is slightly faster, the for loop version is very slow.
Any workaround for faster loops in Python?