Last active
December 18, 2015 19:09
-
-
Save eirenik0/5831159 to your computer and use it in GitHub Desktop.
complex_computation() simulates a slow function. time.sleep(n) causes the program to pause for n seconds. In real life, this might be a call to a database, or a request to another web service.
This file contains hidden or 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 | |
# complex_computation() simulates a slow function. time.sleep(n) causes the | |
# program to pause for n seconds. In real life, this might be a call to a | |
# database, or a request to another web service. | |
def complex_computation(a, b): | |
time.sleep(.5) | |
return a + b | |
# QUIZ - Improve the cached_computation() function below so that it caches | |
# results after computing them for the first time so future calls are faster | |
cache = {} | |
def cached_computation(a, b): | |
key = (a,b) | |
if (a, b) in cache: | |
r = cache[key] | |
else: | |
r = complex_computation(a, b) | |
cache[key] = r | |
return r | |
start_time = time.time() | |
print cached_computation(5, 3) | |
print "the first computation took %f seconds" % (time.time() - start_time) | |
start_time2 = time.time() | |
print cached_computation(5, 3) | |
print "the second computation took %f seconds" % (time.time() - start_time2) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment