From: Summercool Date: 2007-09-16T10:30:30+09:00 Subject: Re: Newbie help On Sep 15, 5:16 pm, Ali Koubeissi wrote: > Hey, I've started with Ruby two days ago, and I have some questions. > > 1- What's the use of Symbols? I've read that symbols are (Symbols are > lightweight strings. Usually, symbols are used in situations where you > need a string but you won't be printing it to the screen.), however I > cannot understand how to use them, and so I tried to read some examples > and I got this: here are two tests: -------------- symbol_vs_string.rb -------------------------------------- i = 0 n = 10000000 hash = {} hash["foobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobar"] = 0 start_time = Time.now while i < n hash["foobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobar"] += 1 i += 1 end finish_time = Time.now puts hash["foobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobar"] puts puts "It took #{finish_time - start_time} seconds to run. #{(n / (finish_time - start_time)).to_i} iterations per second." -------------- symbol_vs_string2.rb -------------------------------------- i = 0 n = 10000000 hash = {} hash[:foobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobar] = 0 start_time = Time.now while i < n hash[:foobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobar] += 1 i += 1 end finish_time = Time.now puts hash[:foobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobar] puts puts "It took #{finish_time - start_time} seconds to run. #{(n / (finish_time - start_time)).to_i} iterations per second." C:\rails\depot>ruby symbol_vs_string.rb 10000000 It took 30.703 seconds to run. 325701 iterations per second. C:\rails\depot>ruby symbol_vs_string2.rb 10000000 It took 11.984 seconds to run. 834445 iterations per second. C:\rails\depot>ruby symbol_vs_string2.rb It took 11.922 seconds to run. 838785 iterations per second. C:\rails\depot>ruby symbol_vs_string.rb 10000000 It took 30.391 seconds to run. 329044 iterations per second.