我看到很有趣的和灾难性的行为与红宝石,见下文红宝石exception.message花费过多时间
class ExceptionTest
def test
@result = [0]*500000
begin
no_such_method
rescue Exception => ex
puts "before #{ex.class}"
st = Time.now
ex.message
puts "after #{Time.now-st} #{ex.message}"
end
end
end
ExceptionTest.new.test
代码理想ex.message
不应该采取任何时间来执行,因此所用时间应在毫秒,但这里是输出
before NameError
after 0.462443 undefined local variable or method `no_such_method' for #<ExceptionTest:0x007fc74a84e4f0>
如果我给你[0]*500000
到一个局部变量,而不是实例变量如result = [0]*500000
它运行正常
before NameError
after 2.8e-05 undefined local variable or method `no_such_method' for #<ExceptionTest:0x007ff59204e518>
看起来莫名其妙ex.message
是循环直通实例变量,为什么会这样做,请赐教!
我试过它在红宝石ruby-1.9.2-p290,ruby-1.9.1-p376,ruby 2.0.0以及在codepad.org上的任何版本的ruby。
编辑:文件中的错误http://bugs.ruby-lang.org/issues/8366
你真的引用了局部变量,所以它的创建没有完全优化吗? – 2013-05-03 23:02:21
@JoachimIsaksson我尝试打印测试方法的开始和结束的第一个和最后一个项目,但仍然与本地var它是快速的,即使它不是,问题仍然如何来ex.message是受所有这些影响 – 2013-05-03 23:04:52
http:///www.mikeperham.com/2012/03/03/the-perils-of-rescue-exception/ – 2013-05-04 01:48:07