Skip to content

Instantly share code, notes, and snippets.

@jasimpson
Last active March 12, 2020 23:57
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save jasimpson/b5a2306fbdfe7b1452f9 to your computer and use it in GitHub Desktop.
Save jasimpson/b5a2306fbdfe7b1452f9 to your computer and use it in GitHub Desktop.
Example code to demonstrate parallel for (parfor) loop implementation using joblib
# Example code to demonstrate parallel for loop implementation using joblib
from joblib import Parallel, delayed
import multiprocessing
# Vars
my_list = range(10)
squares = []
# Function to parallelize
def find_square(i):
return i ** 2
# Without parallel processing
for index, element in enumerate(my_list):
squares.append(find_square(element))
# With parallel processing
num_cores = multiprocessing.cpu_count()
squares = Parallel(n_jobs=num_cores, verbose=50)(delayed(
find_square)(i)for i in my_list)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment