From: Kurt Stephens Date: 2008-11-23T18:11:26+09:00 Subject: [ruby-core:20048] Unexpected Performance of Symbol Construction http://kurtstephens.com/node/72 Evaluating :���foo_bar��� is 1.5 to 2.5 times faster than :foo_bar. This is true for both Ruby 1.8.6-p287 and Ruby 1.9 trunk. The most common means of specifying a Symbol literal is slower than the general expression to create a Symbol from a String constant. # Ruby 1.9 trunk. > ~/local/ruby/trunk/bin/ruby symbol_benchmark.rb user system total real :foo_bar 0.180000 0.000000 0.180000 ( 0.178016) :'foo_bar' 0.070000 0.000000 0.070000 ( 0.072418) :"foo_bar" 0.080000 0.000000 0.080000 ( 0.072390) "foo_bar".to_sym 0.340000 0.000000 0.340000 ( 0.340502) :"foo_#{'bar'}" 0.060000 0.000000 0.060000 ( 0.073382) "foo_#{'bar'}".to_sym 0.350000 0.000000 0.350000 ( 0.349321) # ruby 1.8.6 > ruby symbol_benchmark.rb user system total real :foo_bar 0.370000 0.140000 0.510000 ( 0.513998) :'foo_bar' 0.240000 0.090000 0.330000 ( 0.331501) :"foo_bar" 0.270000 0.060000 0.330000 ( 0.324257) "foo_bar".to_sym 0.480000 0.070000 0.550000 ( 0.573244) :"foo_#{'bar'}" 0.250000 0.070000 0.320000 ( 0.323314) "foo_#{'bar'}".to_sym 0.480000 0.080000 0.560000 ( 0.566108) symbol_benchmark.rb: require 'benchmark' n = 1000000 Benchmark.bm(7) { | x | x.report(':foo_bar' + "\t\t") { n.times { :foo_bar } } x.report(":'foo_bar'" + "\t\t") { n.times { :'foo_bar' } } x.report(':"foo_bar"' + "\t\t") { n.times { :"foo_bar" } } x.report('"foo_bar".to_sym' + "\t") { n.times { "foo_bar".to_sym } } x.report(':"foo_#{\'bar\'}"' + "\t\t") { n.times { :"foo_#{'bar'}" } } x.report('"foo_#{\'bar\'}".to_sym' + "\t") { n.times { "foo_#{'bar'}".to_sym } } }