ray:
speed
Python typically runs all your code on a single core. Even when the program that you're running has components that can easily run in parallel. To make programs faster in parallel scenarios you might want to explore ray. It's not the only tool for this use-case but it's a tool we've come to like.
Notes
To run, ray, you'll first need to make sure it is installed with pip. Once this is accounted for, you can start a ray service.
import ray
ray.init(num_cpus=2);
You can now run the function in parallel.
@ray.remote
def birthday_experiment(class_size, n_sim=1000):
"""Simulates the birthday paradox. Vectorized = Fast!"""
sims = np.random.randint(1, 365 + 1, (n_sim, class_size))
sort_sims = np.sort(sims, axis=1)
n_uniq = (sort_sims[:, 1:] != sort_sims[:, :-1]).sum(axis = 1) + 1
return {"est_prob": np.mean(n_uniq != class_size)}
futures = [birthday_experiment.remote(class_size=size, n_sim=10_000) for size in range(2, 10)]
You should notice a speedup now.
%%time
results = ray.get(futures)
Feedback? See an issue? Something unclear? Feel free to mention it here.
If you want to be kept up to date, consider signing up for the newsletter.