Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
Ruby
What is the difference between :foo and "foo" ?
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
Reply to thread
Message
[QUOTE="ara.t.howard, post: 4498775"] your test did show symbols being faster on my (linux - 2g cpu, 2g ram) machine too btw... but this slightly modified version shows strings being a tiny bit faster: harp:~ > cat a.rb require 'benchmark' n = 2 ** 16 string_hash, symbol_hash = {}, {} Benchmark.bm(10) do |b| b.report("string set"){ n.times{|x| string_hash[rand.to_s.freeze] = rand}} end Benchmark.bm(10) do |b| b.report("symbol set"){ n.times{|x| symbol_hash[rand.to_s.intern] = rand}} end string_keys = string_hash.keys.sort_by{ rand } symbol_keys = symbol_hash.keys.sort_by{ rand } Benchmark.bm(10) do |b| b.report("string get"){ string_keys.each{|k| string_hash[k]}} end Benchmark.bm(10) do |b| b.report("symbol get"){ symbol_keys.each{|k| symbol_hash[k]}} end harp:~ > ./build/ruby-1.8.4/ruby ./a.rb user system total real string set 0.470000 0.000000 0.470000 ( 0.471459) user system total real symbol set 0.640000 0.020000 0.660000 ( 0.661556) user system total real string get 0.100000 0.000000 0.100000 ( 0.101498) user system total real symbol get 0.080000 0.000000 0.080000 ( 0.077205) i think all we are showing here is that there aren't good reasons for one over the other. but that's good to i supose - since people certainly seem to have preferences. cheers. -a -- =============================================================================== | ara [dot] t [dot] howard [at] noaa [dot] gov | all happiness comes from the desire for others to be happy. all misery | comes from the desire for oneself to be happy. | -- bodhicaryavatara =============================================================================== [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
Ruby
What is the difference between :foo and "foo" ?
Top