What is caching?

后端 未结 9 1906
鱼传尺愫
鱼传尺愫 2020-11-28 17:49

I\'m constantly hearing about person y had performance issue x which they solved through caching.

Or, how doing x,y,z in your programs code can hurt your caching abil

9条回答
  •  情话喂你
    2020-11-28 18:25

    Caching does not necessarily only apply to 'oft retrieved' values but to anything you can save time on by reducing the number of times you recompute it. A simple example that comes to mind is calculating the fibonacci sequence. The simplest recursive implementation looks like this (in psuedo-code):

    function f(n)
        if n < 2 then
            return n;
        return f(n - 1) + f(n - 2)
    

    This can be improved with caching to prevent recalculating already known values:

    fib_cache = {}
    
    function f(n)
        if n < 2 then
            return n;
        if fib_cache.contains(n) then
            return fib_cache[n]
        fib_cache[n] = f(n - 1) + f(n - 2)
        return fib_cache[n]
    

提交回复
热议问题