简体   繁体   中英

Can anyone Explain what is memoization?

The following stats are Fibonacci function call stats

Here are some Stats I got after running profiler

[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765]
     57358 function calls (68 primitive calls) in 0.211 seconds

Ordered by: standard name

ncalls  tottime  percall  cumtime  percall filename:lineno(function)
   21    0.000    0.000    0.000    0.000 :0(append)
    1    0.000    0.000    0.210    0.210 :0(exec)
   20    0.000    0.000    0.000    0.000 :0(extend)
    1    0.000    0.000    0.000    0.000 :0(print)
    1    0.001    0.001    0.001    0.001 :0(setprofile)
    1    0.000    0.000    0.210    0.210 <string>:1(<module>)
 21/1    0.000    0.000    0.210    0.210 Fibo1.py:12(fib_seq)
57291/21    0.210    0.000    0.210    0.010 Fibo1.py:3(fib)
    1    0.000    0.000    0.211    0.211 profile:0(print(fib_seq(20)) )
    0    0.000             0.000          profile:0(profiler)

And After using memoization the profiler stats

  [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765]
     147 function calls (89 primitive calls) in 0.002 seconds

 Ordered by: standard name

 ncalls  tottime  percall  cumtime  percall filename:lineno(function)
   21    0.000    0.000    0.000    0.000 :0(append)
    1    0.000    0.000    0.001    0.001 :0(exec)
   20    0.000    0.000    0.000    0.000 :0(extend)
    1    0.000    0.000    0.000    0.000 :0(print)
    1    0.001    0.001    0.001    0.001 :0(setprofile)
    1    0.000    0.000    0.001    0.001 <string>:1(<module>)
   21    0.000    0.000    0.000    0.000 Fibo2.py:16(fib)
 21/1    0.000    0.000    0.001    0.001 Fibo2.py:26(fib_seq)
59/21    0.000    0.000    0.000    0.000 Fibo2.py:9(__call__)
    1    0.000    0.000    0.002    0.002 profile:0(print(fib_seq(20)))
    0    0.000             0.000          profile:0(profiler)

the total function calls reduced by great numbers.If possible please provide some links for more details regarding memoization.

From Wikipedia :: In computing, memoization is an optimization technique used primarily to speed up computer programs by storing the results of expensive function calls and returning the cached result when the same inputs occur again.

https://en.wikipedia.org/wiki/Memoization

In your case, presumably (I'm guessing here but let it ride), each element in your array is being stored as a sum. Without memoization these sums are computed again and again. With memoization it goes down once and never again.

Memoization effectively refers to remembering ("memoization" -> "memorandum" -> to be remembered) results of method calls based on the method inputs and then returning the remembered result rather than computing the result again. You can think of it as a cache for method results. For further details, see page 365 of Cormen et al., Introduction To Algorithms (3e)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM