-
Notifications
You must be signed in to change notification settings - Fork 0
/
memoizetest.py
62 lines (56 loc) · 1.99 KB
/
memoizetest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
from __future__ import print_function
import numpy as np
from datetime import datetime # timing
from rx import Observable
import random
from concurrent.futures import ProcessPoolExecutor
from memoizepy import memoize
import IPython
@memoize
def longrun_eig(mtrx):
""" takes a matrix, returns eigenvalue sum """
eigs = np.linalg.eig(mtrx)[0]
return sum(eigs)
@memoize
def primes_between(a, b):
primes = []
for num in range(a, b):
if all(num % i != 0 for i in range(2, num)):
primes.append(num)
return(len(primes))
def testspeed(num_processes = 6, eigruns = 6):
adim = 300
num_processes = 6
arrays = [np.random.rand(adim * adim).reshape(adim, adim) for _ in range(eigruns)]
print("longrun_eig not memoized:")
now_time = datetime.utcnow()
with ProcessPoolExecutor(num_processes) as executor:
Observable.from_(arrays) \
.flat_map(lambda s: executor.submit(longrun_eig, s)) \
.subscribe(print)
print("time taken {}".format(datetime.utcnow() - now_time))
print()
print("longrun_eig memoized:")
now_time = datetime.utcnow()
Observable.from_(arrays) \
.map(longrun_eig) \
.subscribe(print)
print("time taken {}".format(datetime.utcnow() - now_time))
print()
test_ranges = [(random.randint(1, 5000), random.randint(5001, 15000)) for _ in range(eigruns)]
print("primes_between not memoized:")
now_time = datetime.utcnow()
with ProcessPoolExecutor(num_processes) as executor:
Observable.from_(test_ranges) \
.flat_map(lambda x: executor.submit(primes_between, *x)) \
.subscribe(print)
print("time taken {}".format(datetime.utcnow() - now_time))
print()
print("primes_between memoized:")
now_time = datetime.utcnow()
Observable.from_(test_ranges) \
.map(lambda x: primes_between(*x)) \
.subscribe(print)
print("time taken {}".format(datetime.utcnow() - now_time))
if __name__ == "__main__":
testspeed()