From: Mathieu Bouchard Date: 2001-06-22T15:52:28+09:00 Subject: [ruby-talk:16750] Re: Symbols, and their use as hash keys On Fri, 22 Jun 2001, Aristarkh A Zagorodnikov wrote: > May anyone enlighten me with knowledge of how symbol integer value > (Symbol#to_i) is generated ? It's the integer that would be returned if you were running Ruby 1.4 instead (which had a symbol table, but no Symbol objects) > Also, I'm interested why using it as a hash key is about 5-8 times > slower then string of the same size ? time expr 0.07 s 100000.times{} 0.53 s 100000.times{{}} 1.09 s 100000.times{{1 =>1,2 =>2,3 =>3}} 1.09 s 100000.times{{:x =>1,:y =>2,:z =>3}} 1.68 s 100000.times{{"x"=>1,"y"=>2,"z"=>3}} This test indicates that, for small strings, symbol and integer keys are twice faster than 1-char strings. (note: if you preassign to local variables in the last test, it drops to 1.50 s) I get similar results by assigning to a pre-existing hash. > I understand that Symbols are not intended for this purpose, They are intended for small numbers of small strings that are used often. They can be fast in a hash. However if you use lots and lots of symbols, it may be slower. (note that symbols are *never* garbage-collected, so don't abuse them) matju