On Thu, 13 Jul 2006 15:35:33 +0200, Erik Veenstra <erikveen / gmail.com>  
wrote:

>> > ----------------------------------------------------------------
>> >
>> >  Version 1   100,0%
>> >  Version 2   90,3%
>> >  Version 3   72,7%
>> >  Version 4   70,2%
>> >
>> > ----------------------------------------------------------------
>>
>> Actually version 3 and version 4 are exactly equivalent for
>> Ruby, it parses them both as NODE_IF:
>
> Well, _finally_ the AST is the same. But somehow, it's
> slower... Maybe the translation from source/syntax to AST is
> slower?
>
> The numbers don't lie. They are averages of 3 test runs, with a
> standard deviation of almost zero. (see below for the full
> results.)

Indeed you are right, I forgot the newline nodes:

>> pp <<code.parse_to_nodes.transform(:keep_newline_nodes => true)
if a
   b
else
   c
end
code
[:newline,
  {:next=>
    [:if,
     {:body=>[:newline, {:next=>[:vcall, {:mid=>:b}]}],
      :else=>[:newline, {:next=>[:vcall, {:mid=>:c}]}],
      :cond=>[:vcall, {:mid=>:a}]}]}]
=> nil

vs.

>> pp "a ? b : c".parse_to_nodes.transform(:keep_newline_nodes => true)
[:newline,
  {:next=>
    [:if,
     {:body=>[:vcall, {:mid=>:b}],
      :else=>[:vcall, {:mid=>:c}],
      :cond=>[:vcall, {:mid=>:a}]}]}]
=> nil

The newline nodes are basically no-ops but they still slow things down a  
bit.

In Ruby 1.9 there are no more newline nodes, so there it really is  
equivalent.

Dominik