System.Interactive:Memoize() 和 MemoizeAll() 之间的区别? [英] System.Interactive: Difference between Memoize() and MemoizeAll()?
问题描述
在 Reactive Extensions 的 System.Interactive.dll (v1.0.2521.0) 中,EnumerableEx
有一个 Memoize
方法和一个 MemoizeAll
方法.它们的 API 文档是相同的:
In System.Interactive.dll (v1.0.2521.0) from Reactive Extensions, EnumerableEx
has both a Memoize
method and a MemoizeAll
method. The API documentation is identical for both of them:
创建一个只枚举原始枚举一次并缓存其结果的枚举.
Creates an enumerable that enumerates the original enumerable only once and caches its results.
然而,这些方法显然并不相同.如果我使用 Memoize
,我的 enumerable 在我第一次枚举时有值,第二次似乎是空的.如果我使用 MemoizeAll
那么我会从任何一种方法的描述中得到我期望的行为 - 我可以根据需要多次枚举结果并每次都获得相同的结果,但源只是枚举一次.
However, these methods are clearly not identical. If I use Memoize
, my enumerable has values the first time I enumerate it, and seems to be empty the second time. If I use MemoizeAll
then I get the behavior I would expect from the description of either method - I can enumerate the result as many times as I want and get the same results each time, but the source is only enumerated once.
谁能告诉我这些方法之间的预期区别是什么?Memoize
的用例是什么?这似乎是一种相当无用的方法,而且文档非常混乱.
Can anyone tell me what the intended difference between these methods is? What is the use-case for Memoize
? It seems like a fairly useless method with really confusing documentation.
推荐答案
简单地说,MemoizeAll
只缓存你实际使用的项目——但它永远保留所有缓存的项目.Memoize
立即缓存项目,但最多只能缓存到指定的缓冲区大小.这可以让您在内存使用等方面更有效率,但如果您了解可枚举源的所有使用者将如何使用它,您只想使用后者.
to put it really simply, MemoizeAll
only caches items that you actually use -- but it holds on to all the cached items forever. Memoize
caches items immediately, but only up to the specified buffer size. This lets you be a lot more efficient with memory usage, etc, but you only want to use the latter if you understand how all consumers of the enumerable source will use it.
这篇博文中对差异的解释非常好:http://bartdesmet.net/blogs/bart/archive/2010/01/07/more-linq-with-system-interactive-functional-fun-and-taming-side-effects.aspx一个>
There's a really great explanation of the difference in this blog post: http://bartdesmet.net/blogs/bart/archive/2010/01/07/more-linq-with-system-interactive-functional-fun-and-taming-side-effects.aspx
这篇关于System.Interactive:Memoize() 和 MemoizeAll() 之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!