Generation of Unique Symbols

E

Eric Hofreiter

--0-730992535-1131260619=:58923
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: quoted-printable

So far this is the best way I have come up with to generate unique symbol=
s:
=20
symbol =3D :AAAAAAAA
=20
def getNext
symbol =3D eval(":" + symbol.id2name.next)
end
=20
But this is obviously not very efficient. Is there a better way to do th=
is? If not I may just go back to good old integers.

=09
 
R

Ryan Leavengood

So far this is the best way I have come up with to generate unique symbol= s:

symbol =3D :AAAAAAAA

def getNext
symbol =3D eval(":" + symbol.id2name.next)
end

But this is obviously not very efficient. Is there a better way to do th= is? If not I may just
go back to good old integers.

There is no need for eval:

irb(main):001:0> symbol =3D :AAAAAAAA
=3D> :AAAAAAAA
irb(main):002:0> symbol.to_s.next.intern
=3D> :AAAAAAAB

Ryan
 
R

Ryan Leavengood

But this is obviously not very efficient. Is there a better way to do th= is? 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 =3D :AAAAAAAA
def next_sim1
$sym1 =3D eval(":" + $sym1.id2name.next)
end

$sym2 =3D :AAAAAAAA
def next_sim2
$sym2 =3D $sym2.to_s.next.intern
end

if $0 =3D=3D __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
 
J

Jeremy Kemper

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

# Avoid double-conversion.
$sym3 = 'AAAAAAAA'
def next_sim3
$sym3.succ!.to_sym
end

Best,
jeremy
 
J

James Edward Gray II

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.

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top