From: Ryan Leavengood Date: 2005-11-06T16:32:33+09:00 Subject: Re: Generation of Unique Symbols On 11/6/05, Eric Hofreiter wrote: > > But this is obviously not very efficient. Is there a better way to do this? If not I may just > go back to good old integers. For the fun of it I decided to benchmark your way versus mine. For 100,000 calls here are the results on my reasonably fast Windows XP laptop: user system total real eval 1.766000 0.016000 1.782000 ( 1.782000) intern 0.531000 0.000000 0.531000 ( 0.531000) :AAAAFRYE :AAAAFRYE As you can see using intern is more than 3 times faster. But it took 100,000 calls to even get reasonable comparison numbers, so both are quite fast for normal use. Also as you can see I print out the symbols at the end and that is a pretty interesting symbol after 100,000 iterations :) Here is the code: require 'benchmark' $sym1 = :AAAAAAAA def next_sim1 $sym1 = eval(":" + $sym1.id2name.next) end $sym2 = :AAAAAAAA def next_sim2 $sym2 = $sym2.to_s.next.intern end if $0 == __FILE__ Benchmark.bm(10) do |bx| bx.report('eval') { 100000.times { next_sim1 } } bx.report('intern') { 100000.times { next_sim2 } } end p $sym1 p $sym2 end __END__ Ryan