This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def multiprocess(inputs): | |
""" | |
Compute multiprocess. | |
""" | |
return Parallel(n_jobs=-1)(delayed(f)(x) for x in inputs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def single_thread_with_cache(inputs): | |
""" | |
Compute single threaded with cache. | |
""" | |
f_memoized = cache(f) | |
return [f_memoized(x) for x in inputs] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def single_thread(inputs): | |
""" | |
Compute single threaded. | |
""" | |
return [f(x) for x in inputs] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import multiprocessing | |
from functools import cache | |
from random import randint | |
from joblib import Memory, Parallel, delayed | |
from resc.functions import factors | |
from resc.timer import timefunc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def memoize(func): | |
"""Decorates a function to implement a memo. | |
A simpler, less optimized version of functools.cache.""" | |
memo = {} | |
@functools.wraps(func) | |
def memorize_closure(*args, **kwargs): | |
key = _make_key(args, kwargs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def _make_key(args, kwargs): | |
"""Creates a hashable key. A simplified version of functools._make_key.""" | |
# create a key for the memo from args and kwargs | |
key = args | |
if kwargs: | |
# marks the start of the keyword arguments in key | |
key += (object(),) | |
for item in kwargs.items(): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MemoizedFunction: | |
"""Takes a function and returns a callable that is a memoized version of that function.""" | |
def __init__(self, func): | |
self.func = func | |
self.cache = dict() | |
self.cache_hits = 0 | |
self.n_calls = 0 | |
def __call__(self, *args, **kwargs): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
memo_cache = {} | |
def slow_func(num): | |
if num in memo_cache: | |
return memo_cache[num] | |
print(f"Calculating {num} squared") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
def slow_func(num): | |
print(f"Calculating {num} squared") | |
time.sleep(1) | |
result = num * num | |
return result | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def fibo_rec(num): | |
"""Recursive Fibo.""" | |
return num if num <= 1 else fibo_rec(num - 1) + fibo_rec(num - 2) |