On Nov 23, 2008, at 01:11 , Kurt Stephens wrote:

> http://kurtstephens.com/node/72
>
> Evaluating :=91foo_bar=92 is 1.5 to 2.5 times faster than :foo_bar. =
This =20
> is
> true for both Ruby 1.8.6-p287 and Ruby 1.9 trunk.  The most common =20
> means
> of specifying a Symbol literal is slower than the general expression =20=

> to
> create a Symbol from a String constant.

for at least 1.8 (and I highly suspect 1.9 tho I'm not checking), =20
there is literally no difference between :blah, :'blah', and :"blah" =20
to the parser and therefor the eval loop. You can see that via =20
parse_tree:

> % parse_tree_show -u
> a =3D :blah, :'blah', :"blah"
> s(:lasgn,
>  :a,
>  s(:svalue, s(:array, s(:lit, :blah), s(:lit, :blah), =20
> s(:lit, :blah))))

Likewise, your case with interpolation:

> % parse_tree_show -u
> :"foo_#{'bar'}"
> s(:lit, :foo_bar)

You could assign a local variable with 'bar' and do that instead for a =20=

more fair comparison:

> % parse_tree_show -u
> bar =3D "bar"
> :"foo_#{bar}"
> s(:block,
>  s(:lasgn, :bar, s(:str, "bar")),
>  s(:dsym, "foo_", s(:evstr, s(:lvar, :bar))))

Finally, you NEED to add a null benchmark to your measurements. When =20
your benchmarks are coming that close to the null benchmark, you have =20=

to suspect that your experiment is flawed or your sample size is too =20
small. I'm getting measurements at 1m samples that are only 50% more =20
than null. And as I increase the samples, the number converges DOWN =20
towards null. At 5m it is only 40% more.

> # of iterations =3D 5000000
>                                user     system      total        real
> null_time                  0.810000   0.010000   0.820000 (  0.843928)
> :foo_bar                   1.140000   0.010000   1.150000 (  1.167343)
> :'foo_bar'                 1.170000   0.020000   1.190000 (  1.256334)
> :"foo_bar"                 1.110000   0.010000   1.120000 (  1.191425)
> :"foo_#{'bar'}"            1.130000   0.010000   1.140000 (  1.164772)
> "foo_#{'bar'}".to_sym      2.450000   0.200000   2.650000 (  2.702624)
> "foo_bar".to_sym           2.530000   0.180000   2.710000 (  2.770743)
> :"foo_#{bar}"              4.300000   0.430000   4.730000 (  7.484217)
> "foo_#{bar}".to_sym        5.040000   0.170000   5.210000 (  5.493593)

What this is really saying to me, barring the GC issue you found, is =20
that the numbers for plain symbols don't really matter at all. They're =20=

about as low as they're gonna get.

I do find it interesting that :"foo_#{bar}" is currently 40% slower =20
than "foo_#{bar}".to_sym... I suspect there might be a minor issue =20
there. But for the rest? *shrug* seems acceptable to me.