From: Sylvain Joyeux Date: 2007-07-07T16:41:44+09:00 Subject: Re: Optimizing Symbol#to_proc On Friday 06 July 2007, Nobuyoshi Nakada wrote: > Hi, > > At Sat, 7 Jul 2007 03:06:21 +0900, > > Sylvain Joyeux wrote in [ruby-core:11598]: > > I don't what the "old Ruby version" was, but I tried to implement mine > > and to my surprise my custom version is *much* faster. Does anybody > > know why ? > > > > ========= 2. First naive pure-Ruby version ============== > > class Symbol > > def to_proc > > proc { |x| x.send(self) } > > end > > end > > VM would optimize the block code. What do you mean by that ? It replaces x.send(self) by x.#{send} ? > > ========= 3. Second pure-Ruby version ============== > > class Symbol > > def to_proc > > @proc ||= eval <<-EOD > > proc { |x| x.#{self} } > > EOD > > end > > end > > You added the cached Proc object. It does get faster indeed. Yes, I know that 3 is supposed to be faster than 2 The problem is the comparison with the built-in Ruby version ... The 1. is the one provided by default with Ruby, and it's much slower than the other two. Do you have any explanation ? -- Sylvain Joyeux