Ruby 中的不同记忆技术
如果您是 Ruby 程序员,那么您可能遇到过哈希块记忆模式。作为一个简单的例子,我向您展示斐波那契序列的记忆版本:
fib_hash = Hash.new do |h,i|
h[i] = h[i-1] + h[i-2]
end
# establish the base cases
fib_hash[1] = 1; fib_hash[2] = 1
当然,这不是创建斐波那契序列的记忆版本的唯一方法。您还可以执行以下操作:
@cache = {}; @cache[1] = 1; @cache[2] = 1
def memo_fib(n)
@cache[n] ||= (memo_fib(n-1) + memo_fib(n-2))
end
希望您看到哈希块记忆模式如何映射到第二个版本,这在许多其他语言中更为常见。我想知道这两个版本有什么区别吗?我无法摆脱哈希块版本更高效的感觉,但我无法真正证明原因。
If you are a ruby programmer then you might have come across the hash block memoization pattern. For a simple example I present you the memoized version of the Fibonacci sequence:
fib_hash = Hash.new do |h,i|
h[i] = h[i-1] + h[i-2]
end
# establish the base cases
fib_hash[1] = 1; fib_hash[2] = 1
Of course this is not the only way to create a memoized version of the Fibonacci sequence. You could also do the following:
@cache = {}; @cache[1] = 1; @cache[2] = 1
def memo_fib(n)
@cache[n] ||= (memo_fib(n-1) + memo_fib(n-2))
end
Hopefully you see how the hash block memoization pattern maps to the second version which is much more common in many other languages. What I would like to know is if there is any difference between the two versions? I can't shake the feeling that the hash block version is more efficient but I can't really justify why.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
MRI 1.8.7 中的基准测试结果为:
在 MRI 1.9.0 中:
基准测试是从 1 计算斐波那契数的 100 次迭代到 1000,在每次迭代开始时重置哈希或缓存。
这是哈希块的基准:
这是非哈希块的基准:
A benchmark in MRI 1.8.7 yields:
And in MRI 1.9.0:
The benchmark is 100 iterations of computing the Fibonacci numbers from 1 through 1000, resetting the hash or cache at the start of each iteration.
Here's the benchmark for the hash block:
Here's the benchmark for the non hash block: