Maybe, but people are always talking about using symbols to speed up hashes,
etc.... And symbols *can* act that way, so not accomodating it in some way
would be somewhat arbitrary.
i've always assumed this to be true too, but:
jib:~ > ruby a.rb 8192
---
-
Symbol:
max: "0.0039439201354981"
avg: "0.0000033703981899"
min: "0.0000019073486328"
-
String:
max: "0.0023880004882812"
avg: "0.0000032874231692"
min: "0.0000019073486328"
jib:~ > cat a.rb
require 'tempfile'
require 'tmpdir'
require 'fileutils'
require 'yaml'
class HashProfiler
PATHS = {
String => File.join(Dir.tmpdir, 'string'),
Symbol => File.join(Dir.tmpdir, 'symbol'),
}
at_exit{ PATHS.map{|t,p| FileUtils.rm_f p} }
class << self
def stats
list = []
PATHS.each do |type,path|
times = IO.readlines(path).map{|line| Float line}
avg = times.inject(0.0){|a,f| a += f} / times.size.to_f
list << Hash[
type.to_s => {
'min' => ('%16.16f' % times.min),
'max' => ('%16.16f' % times.max),
'avg' => ('%16.16f' % avg),
}
]
end
list
end
end
def initialize type, n
@type = type
@n = n
populate
gen_lookups
end
def populate
@h = {}
@n.times do |i|
key = "foobar#{ i }"
@h[(String == @type ? key : key.intern)] = rand
end
@keys = @h.keys
@size = @keys.size
end
def gen_lookups
@lookups = []
@n.times{|i| @lookups << @keys[rand(@size)]}
end
def profile
fork do
GC.disable
open(PATHS[@type],'a+') do |f|
@lookups.each do |k|
a = Time.now.to_f
v = @h[k]
b = Time.now.to_f
t = b - a
f.puts t
end
end
exit!
end
Process.wait
end
end
STDOUT.sync = true
n = Integer(ARGV.shift || 2 ** 13)
[String, Symbol].each do |type|
profiler = HashProfiler.new type, n
4.times{ profiler.profile }
end
y HashProfiler.stats
this suprises me - they look to be about the same. perhaps my code has a bug.
regards.
-a
--
===============================================================================
| EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
| PHONE :: 303.497.6469
| A flower falls, even though we love it;
| and a weed grows, even though we do not love it.
| --Dogen
===============================================================================