【发布时间】:2014-05-03 14:05:41
【问题描述】:
在尝试一些记忆技术时,我偶然发现了这个基准测试结果,这与我的预期相反。似乎我犯了一些愚蠢的错误,有人看到我在这里做错了吗(基准对记忆和非记忆代码给出了相似的结果)?
require 'benchmark'
# -----------------------------------------
class FactorialClass
@@sequence = [1]
def self.of( n )
@@sequence[n] || n * of( n - 1 )
end
end
# -----------------------------------------
def factorial_with_memoization
sequence = [1]
lambda = Proc.new do |n|
sequence[n] || n * lambda.call( n - 1 )
end
end
f = factorial_with_memoization()
# -----------------------------------------
def factorial n
n == 0 ? 1 : n * factorial( n - 1 )
end
# -----------------------------------------
count = 1_000
n = 1000
without_memoization = Benchmark.measure do
count.times do
factorial(n)
end
end
with_memoization_lambda = Benchmark.measure do
count.times do
f.call(n)
end
end
with_memoization_class = Benchmark.measure do
count.times do
FactorialClass.of(n)
end
end
puts "Without memoization : #{ without_memoization }"
puts "With memoization using lambda : #{ with_memoization_lambda }"
puts "With memoization using class : #{ with_memoization_class }"
** 结果是:**
Without memoization : 1.210000 0.100000 1.310000 ( 1.309675)
With memoization using lambda : 1.750000 0.100000 1.850000 ( 1.858737)
With memoization using class : 1.270000 0.090000 1.360000 ( 1.358375)
【问题讨论】:
-
我不认为你会记住任何东西......
-
我相信缓存不起作用?你能解释一下什么是错的吗?
-
对于 n 的阶乘,您最终会计算 n 阶乘。所以你不会记住任何东西。您永远不会“存储”任何结果。
标签: ruby memoization