memoization

Storing calculated values in an object

大兔子大兔子 提交于 2019-11-28 05:04:55
问题 Recently I've been writing a bunch of code like this: class A: def __init__(self, x): self.x = x self._y = None def y(self): if self._y is None: self._y = big_scary_function(self.x) return self._y def z(self, i): return nice_easy_function(self.y(), i) In a given class I may have a number of things working like this y , and I may have other things that use the stored pre-calculated values. Is this the best way to do things or would you recommend something different? Note that I don't pre

What is memoization good for and is it really all that helpful?

亡梦爱人 提交于 2019-11-28 04:57:13
There are a few automatic memoization libraries available on the internet for various different languages; but without knowing what they are for, where to use them, and how they work, it can be difficult to see their value. What are some convincing arguments for using memoization, and what problem domain does memoization especially shine in? Information for the uninformed would be especially appreciated here. The popular factorial answer here is something of a toy answer. Yes, memoization is useful for repeated invocations of that function, but the relationship is trivial — in the "print

How can a recursive Java method be memoized?

大憨熊 提交于 2019-11-28 04:43:13
问题 So I've built this program to build different stair cases. Essentially the problem is: Given an integer N, how many different ways can you build the staircase. N is guaranteed to be larger than 3 and smaller than 200. Any previous step can not be larger than its following step otherwise it defeats the purpose of the staircase. So given N = 3 You can build one staircase: 2 steps and then 1 step following that Given N = 4 You can build one staircase: 3 steps and then 1 step following that Given

Thread-safe memoization

雨燕双飞 提交于 2019-11-28 04:10:33
Let's take Wes Dyer's approach to function memoization as the starting point: public static Func<A, R> Memoize<A, R>(this Func<A, R> f) { var map = new Dictionary<A, R>(); return a => { R value; if (map.TryGetValue(a, out value)) return value; value = f(a); map.Add(a, value); return value; }; } The problem is, when using it from multiple threads, we can get into trouble: Func<int, int> f = ... var f1 = f.Memoize(); ... in thread 1: var y1 = f1(1); in thread 2: var y2 = f1(1); // We may be recalculating f(1) here! Let's try to avoid this. Locking on map : public static Func<A, R> Memoize<A, R>

What are the different techniques for memoization in Java? [closed]

两盒软妹~` 提交于 2019-11-28 03:51:14
I know of this one http://onjava.com/pub/a/onjava/2003/08/20/memoization.html but is there anything else? lacroix1547 Memoization is also easy with plain simple typesafe Java. You can do it from scratch with the following reusable classes. I use these as caches whose lifespan are the request on a webapp. Of course use the Guava MapMaker if you need an eviction strategy or more features like synchronization. If you need to memoize a method with many parameters, just put the parameters in a list with both techniques, and pass that list as the single parameter. abstract public class Memoize0<V> {

Python lazy evaluator

扶醉桌前 提交于 2019-11-28 03:40:10
问题 Is there a Pythonic way to encapsulate a lazy function call, whereby on first use of the function f() , it calls a previously bound function g(Z) and on the successive calls f() returns a cached value? Please note that memoization might not be a perfect fit. I have: f = g(Z) if x: return 5 elif y: return f elif z: return h(f) The code works, but I want to restructure it so that g(Z) is only called if the value is used. I don't want to change the definition of g(...) , and Z is a bit big to

When to use memoization in Ruby on Rails

删除回忆录丶 提交于 2019-11-28 03:24:15
In mid July 2008 Memoization was added to Rails core. A demonstration of the usage is here . I have not been able to find any good examples on when methods should be memoized, and the performance implications of each. This blog post , for example, suggests that oftentimes, memoization should not be used at all. For something that could potentially have tremendous performance implications, there seem to be few resources that go beyond providing a simple tutorial. Has anyone seen memoization used in their own projects? What factors would make you consider memoizing a method? After doing some

Java memoization method

孤者浪人 提交于 2019-11-28 02:55:47
问题 I came across an interesting problem and was wondering if and how could this be done in Java: Create a method which can memoize any function/method . The method has the following arguments : the method/function and the argument(s) for it. For example let's say i have this method : int addOne(int a) { return a + 1;} and i call my memoization method two times with the same arguments : addOne and 5 for example, the first call should actually call the addOne method and return the result and also

Java: automatic memoization

纵然是瞬间 提交于 2019-11-27 21:39:57
问题 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 or do something else so I get this automatically on those functions where I want it? 回答1: Spring 3.1 now provides a @Cacheable annotation, which does exactly this. As the name implies, @Cacheable is used to demarcate methods that are cacheable -

How can I memoize a class instantiation in Python?

こ雲淡風輕ζ 提交于 2019-11-27 19:47:02
Ok, here is the real world scenario: I'm writing an application, and I have a class that represents a certain type of files (in my case this is photographs but that detail is irrelevant to the problem). Each instance of the Photograph class should be unique to the photo's filename. The problem is, when a user tells my application to load a file, I need to be able to identify when files are already loaded, and use the existing instance for that filename, rather than create duplicate instances on the same filename. To me this seems like a good situation to use memoization, and there's a lot of