Skip to content

Instantly share code, notes, and snippets.

@AruniRC
Forked from jasimpson/python-parfor.py
Created February 6, 2018 13:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AruniRC/49b87469e45358ca571594752908ba0a to your computer and use it in GitHub Desktop.
Save AruniRC/49b87469e45358ca571594752908ba0a 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