Faster ruby
After reading a post on symbols, I went on doing my own experiment. Here’s what came out:
h[:cool] = :hello
will execute approximately 2 times as fast as:
h["cool"] = :hello
The original post pointed out that symbols are never garbage collected but given the low memory footprint of Ruby, might as well get the extra speed.
An other experiment I did some time ago, if you declare:
module Niko MY_CONST = “this is a string for testing” end
and then try to access it:
Niko::MY_CONST
will be slightly slower than doing:
include Niko MY_CONST
Amusingly enough, this appears to be untrue on jruby. (confirmed that the same behavior is on both plateform, see next post)

The second example is somewhat surprising to me. In the first case, if Niko is a constant in the current module/class namespace, it will do a single lookup to find it, followed by a single lookup in the Niko namespace to find MY_CONST. In the second example, it will first search the current module or class for MY_CONST and then search the included module for MY_CONST. Both cases work out to two constant hash lookups, so it’s strange that one would be faster in Ruby than the other…
Charles,
Thank you for your comments and insight.
I went back and ran the experiment again:
http://hellohellonico.wordpress.com/2007/07/20/faster-ruby2
I wonder what makes those two tests behaving differently from a jruby point of view.