Joblib Simple Example Parallel Example Slower Than Simple
from math import sqrt from joblib import Parallel, delayed import time if __name__ == '__main__': st= time.time() #[sqrt(i ** 2) for i in range(100000)] #this part in no
Solution 1:
Parallel processes (which joblib
creates) require copying data. Imagine it this way: you have two people who each carry a rock to their house, shine it, then bring it back. That's loads slower than one person shining them on-the-spot.
All the time is wasted in transit, rather than being spent on the actual calculation. You will only benefit from parallel processes for more substantial computational tasks.
If you care about speeding up this specific operation:
Use numpy
's vectorized math operations. On my machine, parallel: 1.13 s, serial: 54.6 ms, numpy: 3.74 ms.
a = np.arange(100000, dtype=np.int)
np.sqrt(a ** 2)
Don't worry about libraries like Cython or Numba; they won't speed up this already performant operation.
Post a Comment for "Joblib Simple Example Parallel Example Slower Than Simple"