From: Jonathan Simms Date: 2005-12-30T01:51:31+09:00 Subject: Re: What is the difference between :foo and "foo" ? On Dec 29, 2005, at 12:22 AM, Jim Weirich wrote: > ara wrote: >> but this slightly modified version shows strings being a tiny bit >> faster: > > The difference is that your version measures more than just hash > access > speed. It also includes string and symbol creation times. In > particular, to create a symbol, you must first create a string, so you > have twice as many object creations when using symbols. > This version (I believe) shows that by using a string literal, you're paying for the creation of a string object with each invocation, whereas by using a symbol, you only pay for the interning once, but on each subsequent usage, you're using a reference to a singleton. slyphon@willie ~ $ cat /tmp/bmrk.rb #!/usr/bin/env ruby require 'benchmark' n = 1_000_000 string_hash, symbol_hash = {}, {} Benchmark.bm(10) do |b| b.report("string set"){ n.times{|x| string_hash["foo"] = rand}} end Benchmark.bm(10) do |b| b.report("symbol set"){ n.times{|x| symbol_hash[:foo] = rand}} end slyphon@willie ~ $ ruby /tmp/bmrk.rb user system total real string set 1.270000 0.000000 1.270000 ( 1.274019) user system total real symbol set 0.880000 0.000000 0.880000 ( 0.877080) slyphon@willie ~ $ - Jonathan Simms