From d9c8a56cfefe7c5cce734153f33c8af149bf61c4 Mon Sep 17 00:00:00 2001 From: Anna Yasenova Date: Sun, 19 Feb 2017 21:16:59 +0200 Subject: [PATCH 1/3] Using Map. Testing with different data --- JavaScript/2-speed.js | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/JavaScript/2-speed.js b/JavaScript/2-speed.js index de89ff8..aca41d3 100644 --- a/JavaScript/2-speed.js +++ b/JavaScript/2-speed.js @@ -3,14 +3,15 @@ const LOOP = 10000; function memoize(fn) { - const cache = {}; + const cache = new Map(); return (...args) => { const key = args + ''; - const val = cache[key]; - if (val) return val; + if (cache.has(key)) { + return cache.get(key); + } else { const res = fn(...args); - cache[key] = res; + cache.set(key, res); return res; } }; @@ -20,7 +21,8 @@ let fib = n => (n <= 2) ? 1 : fib(n - 1) + fib(n - 2); function speedTest(name, fn, args, count) { let start = new Date().getTime(); - for (let i = 0; i < count; i++) { + let i; //or use "var" in for(...). (no) + for (i = 0; i < count; i++) { fn(...args); } let end = new Date().getTime(); @@ -28,6 +30,12 @@ function speedTest(name, fn, args, count) { console.log(`${name} * ${count} : ${time}`); } +speedTest('fib(10)', fib, [10], LOOP); +speedTest('fib(15)', fib, [15], LOOP); speedTest('fib(20)', fib, [20], LOOP); +speedTest('fib(25)', fib, [25], LOOP); fib = memoize(fib); +speedTest('memoized fib(10)', fib, [10], LOOP); +speedTest('memoized fib(15)', fib, [15], LOOP); speedTest('memoized fib(20)', fib, [20], LOOP); +speedTest('memoized fib(25)', fib, [25], LOOP); \ No newline at end of file From 231e421bb58a46ba682964f0b85399cd5651f4ce Mon Sep 17 00:00:00 2001 From: Anna Yasenova Date: Wed, 10 Jan 2018 15:50:53 +0200 Subject: [PATCH 2/3] Implemented python memoization. --- Python/1-fibonacci-simple.py | 15 ++++++++++++++ Python/2-fibonacci-memoize.py | 20 +++++++++++++++++++ Python/3-fibonacci-decorator.py | 20 +++++++++++++++++++ Python/4-fibonacci-class.py | 26 ++++++++++++++++++++++++ Python/5-memoize-file.py | 34 ++++++++++++++++++++++++++++++++ Python/6-memoize-lru.py | 13 ++++++++++++ Python/memoize.pkl | Bin 0 -> 22 bytes 7 files changed, 128 insertions(+) create mode 100644 Python/1-fibonacci-simple.py create mode 100644 Python/2-fibonacci-memoize.py create mode 100644 Python/3-fibonacci-decorator.py create mode 100644 Python/4-fibonacci-class.py create mode 100644 Python/5-memoize-file.py create mode 100644 Python/6-memoize-lru.py create mode 100644 Python/memoize.pkl diff --git a/Python/1-fibonacci-simple.py b/Python/1-fibonacci-simple.py new file mode 100644 index 0000000..b606197 --- /dev/null +++ b/Python/1-fibonacci-simple.py @@ -0,0 +1,15 @@ +def fib(n): + if n == 0: + return 0 + elif n == 1: + return 1 + else: + return fib(n-1) + fib(n-2) + +print('fib(0): ', fib(0)) +print('fib(1): ', fib(1)) +print('fib(3): ', fib(3)) +print('fib(7): ', fib(7)) +print('fib(10): ', fib(10)) + +print('fib(40): ', fib(40)) # here is a problem \ No newline at end of file diff --git a/Python/2-fibonacci-memoize.py b/Python/2-fibonacci-memoize.py new file mode 100644 index 0000000..b6edb79 --- /dev/null +++ b/Python/2-fibonacci-memoize.py @@ -0,0 +1,20 @@ +def memoize(f): + cache = {} + def inMemo(x): + if x not in cache: + cache[x] = f(x) + return cache[x] + return inMemo + + +def fib(n): + if n == 0: + return 0 + elif n == 1: + return 1 + else: + return fib(n-1) + fib(n-2) + + +fib = memoize(fib) +print('fib(40): ', fib(40)) \ No newline at end of file diff --git a/Python/3-fibonacci-decorator.py b/Python/3-fibonacci-decorator.py new file mode 100644 index 0000000..be436ab --- /dev/null +++ b/Python/3-fibonacci-decorator.py @@ -0,0 +1,20 @@ +def memoize(f): + cache = {} + def inMemo(x): + if x not in cache: + cache[x] = f(x) + return cache[x] + return inMemo + + +@memoize +def fib(n): + if n == 0: + return 0 + elif n == 1: + return 1 + else: + return fib(n-1) + fib(n-2) + + +print('fib(40): ', fib(40)) \ No newline at end of file diff --git a/Python/4-fibonacci-class.py b/Python/4-fibonacci-class.py new file mode 100644 index 0000000..7deb139 --- /dev/null +++ b/Python/4-fibonacci-class.py @@ -0,0 +1,26 @@ +class Memoize: + def __init__(self, func): + self.func = func + self.cache = {} + def __call__(self, *args): + if args not in self.cache: + self.cache[args] = self.func(*args) + return self.cache[args] + +@Memoize +def fib(n): + if n == 0: + return 0 + elif n == 1: + return 1 + else: + return fib(n-1) + fib(n-2) + + +print('fib(0): ', fib(0)) +print('fib(1): ', fib(1)) +print('fib(3): ', fib(3)) +print('fib(7): ', fib(7)) +print('fib(10): ', fib(10)) + +print('fib(40): ', fib(40)) \ No newline at end of file diff --git a/Python/5-memoize-file.py b/Python/5-memoize-file.py new file mode 100644 index 0000000..9f33f3f --- /dev/null +++ b/Python/5-memoize-file.py @@ -0,0 +1,34 @@ +import os, pickle +from functools import wraps + +def memoize(fn): + if os.path.exists('memoize.pkl'): + print('Reading cache file!') + with open('memoize.pkl') as f: + cache = pickle.load(f) + else: + cache = {} + + @wraps(fn) + def wrap(*args): + if args not in cache: + print('Running function with argument', args[0]) + cache[args] = fn(*args) + # update the cache file + with open('memoize.pkl', 'wb') as f: + pickle.dump(cache, f) + else: + print('Result in cache!') + return cache[args] + return wrap + +@memoize +def sqrt(x): + return x**2 + + +print(sqrt(5)) +print(sqrt(5)) +print(sqrt(7)) +print(sqrt(5)) +print(sqrt(7)) \ No newline at end of file diff --git a/Python/6-memoize-lru.py b/Python/6-memoize-lru.py new file mode 100644 index 0000000..08b48a5 --- /dev/null +++ b/Python/6-memoize-lru.py @@ -0,0 +1,13 @@ +import functools + +@functools.lru_cache() +def fib_lru(n): + if n in (0, 1): + return n + else: + return fib_lru(n - 2) + fib_lru(n - 1) + +print('fib_lru(0): ', fib_lru(0)) +print('fib_lru(7): ', fib_lru(7)) +print('fib_lru(10): ', fib_lru(10)) +print('fib_lru(40): ', fib_lru(40)) \ No newline at end of file diff --git a/Python/memoize.pkl b/Python/memoize.pkl new file mode 100644 index 0000000000000000000000000000000000000000..12be9f8b8d3e3bc4fa62f66e7fc82f0e883ac620 GIT binary patch literal 22 dcmZo*t}SHH@Mdo Date: Thu, 11 Jan 2018 09:06:58 +0200 Subject: [PATCH 3/3] New line added. --- Python/1-fibonacci-simple.py | 2 +- Python/2-fibonacci-memoize.py | 2 +- Python/3-fibonacci-decorator.py | 2 +- Python/4-fibonacci-class.py | 2 +- Python/5-memoize-file.py | 2 +- Python/6-memoize-lru.py | 2 +- 6 files changed, 6 insertions(+), 6 deletions(-) diff --git a/Python/1-fibonacci-simple.py b/Python/1-fibonacci-simple.py index b606197..9b5ba88 100644 --- a/Python/1-fibonacci-simple.py +++ b/Python/1-fibonacci-simple.py @@ -12,4 +12,4 @@ def fib(n): print('fib(7): ', fib(7)) print('fib(10): ', fib(10)) -print('fib(40): ', fib(40)) # here is a problem \ No newline at end of file +print('fib(40): ', fib(40)) # here is a problem diff --git a/Python/2-fibonacci-memoize.py b/Python/2-fibonacci-memoize.py index b6edb79..47226d5 100644 --- a/Python/2-fibonacci-memoize.py +++ b/Python/2-fibonacci-memoize.py @@ -17,4 +17,4 @@ def fib(n): fib = memoize(fib) -print('fib(40): ', fib(40)) \ No newline at end of file +print('fib(40): ', fib(40)) diff --git a/Python/3-fibonacci-decorator.py b/Python/3-fibonacci-decorator.py index be436ab..6070e83 100644 --- a/Python/3-fibonacci-decorator.py +++ b/Python/3-fibonacci-decorator.py @@ -17,4 +17,4 @@ def fib(n): return fib(n-1) + fib(n-2) -print('fib(40): ', fib(40)) \ No newline at end of file +print('fib(40): ', fib(40)) diff --git a/Python/4-fibonacci-class.py b/Python/4-fibonacci-class.py index 7deb139..b4874c5 100644 --- a/Python/4-fibonacci-class.py +++ b/Python/4-fibonacci-class.py @@ -23,4 +23,4 @@ def fib(n): print('fib(7): ', fib(7)) print('fib(10): ', fib(10)) -print('fib(40): ', fib(40)) \ No newline at end of file +print('fib(40): ', fib(40)) diff --git a/Python/5-memoize-file.py b/Python/5-memoize-file.py index 9f33f3f..de78d03 100644 --- a/Python/5-memoize-file.py +++ b/Python/5-memoize-file.py @@ -31,4 +31,4 @@ def sqrt(x): print(sqrt(5)) print(sqrt(7)) print(sqrt(5)) -print(sqrt(7)) \ No newline at end of file +print(sqrt(7)) diff --git a/Python/6-memoize-lru.py b/Python/6-memoize-lru.py index 08b48a5..6da5323 100644 --- a/Python/6-memoize-lru.py +++ b/Python/6-memoize-lru.py @@ -10,4 +10,4 @@ def fib_lru(n): print('fib_lru(0): ', fib_lru(0)) print('fib_lru(7): ', fib_lru(7)) print('fib_lru(10): ', fib_lru(10)) -print('fib_lru(40): ', fib_lru(40)) \ No newline at end of file +print('fib_lru(40): ', fib_lru(40))