So far this is the best way I have come up with to generate unique symbols:
symbol = :AAAAAAAA
def getNext
symbol = eval(":" + symbol.id2name.next)
end
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.
···
---------------------------------
Yahoo! FareChase - Search multiple travel sites in one click.
There is no need for eval:
irb(main):001:0> symbol = :AAAAAAAA
=> :AAAAAAAA
irb(main):002:0> symbol.to_s.next.intern
=> :AAAAAAAB
Ryan
···
On 11/6/05, Eric Hofreiter <erichof425@yahoo.com> wrote:
So far this is the best way I have come up with to generate unique symbols:
symbol = :AAAAAAAA
def getNext
symbol = eval(":" + symbol.id2name.next)
end
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
···
On 11/6/05, Eric Hofreiter <erichof425@yahoo.com> 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.
Others have answered this, but let me add a warning. I'm not sure how many of these symbols you are generating, but be aware that symbols are not garbage collected. Watch your memory consumption.
James Edward Gray II
···
On Nov 6, 2005, at 1:03 AM, Eric Hofreiter wrote:
So far this is the best way I have come up with to generate unique symbols:
symbol = :AAAAAAAA
def getNext
symbol = eval(":" + symbol.id2name.next)
end
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.
# Avoid double-conversion.
$sym3 = 'AAAAAAAA'
def next_sim3
$sym3.succ!.to_sym
end
Best,
jeremy
···
On Nov 5, 2005, at 11:32 PM, Ryan Leavengood wrote:
On 11/6/05, Eric Hofreiter <erichof425@yahoo.com> 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:
[...]
$sym1 = :AAAAAAAA
def next_sim1
$sym1 = eval(":" + $sym1.id2name.next)
end
$sym2 = :AAAAAAAA
def next_sim2
$sym2 = $sym2.to_s.next.intern
end