ara wrote: > i see this claim all the time but never data supporting it, all my test > programs have shown the opposite to be true. My benchmark shows symbols to have a slight edge. I've attached my benchmark at the end for review. (Benchmarks are tricky ... sometimes you don't measure what you think you are measuring). $ ruby string_symbol_time.rb Strings Filling 14.080000 0.090000 14.170000 ( 14.406058) Strings Fetching 4.320000 0.030000 4.350000 ( 4.355025) Symbols Filling 12.300000 0.030000 12.330000 ( 12.561648) Symbols Fetching 3.370000 0.030000 3.400000 ( 3.461109) > also, don't forget that symbols are __never__ freed. True, but when used properly, this is rarely a concern. If they are used as programmer names for things, then the number of symbols is finite and not likely to grow and consume memory as the program runs. If however, you are dynamically creating symbols by interning strings, I would suggest you review your use of symbols and consider using strings instead. -- -- Jim Weirich ------------------------------------------------------------------- #!/usr/bin/env ruby require 'benchmark' SIZE = 100 N = 10000 def make_str_keys (1..SIZE).collect { |i| "key#{i}" } end def make_sym_keys(strs) strs.collect { |s| s.intern } end def populate(keys) result = {} keys.each_with_index do |k, i| result[k] = i end result end def fetch(keys, hash) keys.each do |key| hash[key] end end strs = make_str_keys syms = make_sym_keys(strs) str_hash = populate(strs) sym_hash = populate(syms) puts "Strings Filling" puts Benchmark.measure { N.times do populate(strs) end } puts "Strings Fetching" puts Benchmark.measure { N.times do fetch(strs, str_hash) end } puts puts "Symbols Filling" puts Benchmark.measure { N.times do populate(syms) end } puts "Symbols Fetching" puts Benchmark.measure { N.times do fetch(syms, sym_hash) end } -- Posted via http://www.ruby-forum.com/.