The nature, benefits and drawbacks of caching.
2.1.2 Thinking ahead
Thinking ahead — Note 3 of 4
Candidates need to understand the purpose of caching in programming and how it can be used when writing a program. Candidates need to be able to apply caching to a scenario and understand the benefits and drawbacks of using it.
Caching stores data that has been used recently, or is likely to be needed soon, in a location that is faster to access. It is an example of thinking ahead, used in processors, web browsers and programs.
Exam tipCommon mistake
Temporarily storing data or instructions that have been used, or are likely to be needed, so they can be accessed more quickly next time.
cache = {}
def slow_square(n):
if n in cache: # cache hit: return the stored result
return cache[n]
result = n * n # imagine this takes a long time
cache[n] = result # store for next time
return result