Questions tagged [memoization]

In computing, memoization is an optimization technique used primarily to speed up computer programs by having function calls avoid repeating the calculation of results for previously-processed inputs.

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.

This tag should be used for programs that are using memoization.

Source

Wikipedia

1491 questions
20
votes
5 answers

Java: automatic memoization

I have a few functions in my code where it makes much sense (seems even mandatory) to use memoization. I don't want to implement that manually for every function separately. Is there some way (for example like in Python) I can just use an annotation…
Albert
  • 65,406
  • 61
  • 242
  • 386
20
votes
5 answers

What type to use to store an in-memory mutable data table in Scala?

Each time a function is called, if it's result for a given set of argument values is not yet memoized I'd like to put the result into an in-memory table. One column is meant to store a result, others to store arguments values. How do I best…
Ivan
  • 63,011
  • 101
  • 250
  • 382
19
votes
5 answers

How to memoize **kwargs?

I haven't seen an established way to memoize a function that takes key-word arguments, i.e. something of type def f(*args, **kwargs) since typically a memoizer has a dict to cache results for a given set of input parameters, and kwargs is a dict…
juanchopanza
  • 223,364
  • 34
  • 402
  • 480
19
votes
2 answers

How can I memoize a method that may return true, false, or nil in Ruby?

Obviously ||= won't work def x? @x_query ||= expensive_way_to_calculate_x end because if it turns out to be false or nil, then expensive_way_to_calculate_x will get run over and over. Currently the best way I know is to put the value into an…
Seamus Abshere
  • 8,326
  • 4
  • 44
  • 61
18
votes
2 answers

What is the lifetime of a memoized value in a functional language like Haskell?

In a pure functional language with lazy semantics (such as Haskell), results of computations are memoized so that further evaluations of a function with the same inputs do not recompute the value but get it directly from the cache of memoized…
Gabriel Cuvillier
  • 3,617
  • 1
  • 28
  • 35
18
votes
4 answers

How does DP helps if there are no overlapping in sub problems [0/1 knapsack]

Consider below inputs for typical Knapsack problem. V = [10,8,12] W = [2,3,7] i = 1,2,3 C = 10 I tried recursion with memoization to solve this sample but found no overlapping sub problem. Signature of the recursive procedure : knapsack(int c,…
18
votes
8 answers

How do I memoize a recursive function in Lisp?

I'm a Lisp beginner. I'm trying to memoize a recursive function for calculating the number of terms in a Collatz sequence (for problem 14 in Project Euler). My code as of yet is: (defun collatz-steps (n) (if (= 1 n) 0 (if (evenp n) …
Sundar R
  • 13,776
  • 6
  • 49
  • 76
18
votes
4 answers

Memoization algorithm time complexity

I read this article Retiring a Great Interview Problem, the author came up with a "word break" problem and gave three solutions. The efficient one uses memoization algorithm and the author said its worst case time complexity is O(n^2) since "the key…
mitchelllc
  • 1,607
  • 4
  • 20
  • 24
17
votes
2 answers

Optimization of Function Calls in Haskell

Not sure what exactly to google for this question, so I'll post it directly to SO: Variables in Haskell are immutable Pure functions should result in same values for same arguments From these two points it's possible to deduce that if you call…
user500944
17
votes
5 answers

What does this C++11 code (memoize) do?

I found an article that contains this code: template std::function memoize(std::function func) { std::map, ReturnType> cache; return…
Mircea Ispas
  • 20,260
  • 32
  • 123
  • 211
17
votes
4 answers

Is dynamic programming backtracking with cache

I've always wondered about this. And no books state this explicitly. Backtracking is exploring all possibilities until we figure out one possibility cannot lead us to a possible solution, in that case we drop it. Dynamic programming as I understand…
Mohan
  • 1,850
  • 1
  • 19
  • 42
17
votes
4 answers

Does Python intern strings?

In Java, explicitly declared Strings are interned by the JVM, so that subsequent declarations of the same String results in two pointers to the same String instance, rather than two separate (but identical) Strings. For example: public String baz()…
csvan
  • 8,782
  • 12
  • 48
  • 91
16
votes
4 answers

Two parameter memoization in Haskell

I'm trying to memoize the following function: gridwalk x y | x == 0 = 1 | y == 0 = 1 | otherwise = (gridwalk (x - 1) y) + (gridwalk x (y - 1)) Looking at this I came up with the following solution: gw :: (Int -> Int -> Int) -> Int ->…
Philip Kamenarsky
  • 2,757
  • 2
  • 24
  • 30
16
votes
3 answers

How do I memoize expensive calculations on Django model objects?

I have several TextField columns on my UserProfile object which contain JSON objects. I've also defined a setter/getter property for each column which encapsulates the logic for serializing and deserializing the JSON into python datastructures. The…
David Eyk
  • 12,171
  • 11
  • 63
  • 103
16
votes
2 answers

Choosing between continuation passing style and memoization

While writing up examples for memoization and continuation passing style (CPS) functions in a functional language, I ended up using the Fibonacci example for both. However, Fibonacci doesn't really benefit from CPS, as the loop still has to run…
Abel
  • 56,041
  • 24
  • 146
  • 247