0

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?

arajshree
  • 626
  • 4
  • 13
  • 1
    Yes, indeed, python loops at the interpreter level will not be as fast as vectorized numpy array operations, that is why `numpy` exists. try your best to avoid the loops, but also consider `numba` if your operations involve `numpy.ndarray` objects, because `numba` will JIT compile code to be much faster. – juanpa.arrivillaga Jun 28 '19 at 02:59
  • Python was never intended to do fast numerical computations. – Adam Jun 28 '19 at 03:14

1 Answers1

0

Similar for-loop comparison with Java is made HERE. The explanation using for loops in python and (optimized) python packages may be insightful.

Rok Petric
  • 75
  • 3
  • 9