Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Re: Perl Idioms Explained - keys %{{map{$_=>1}@list}}

by diotalevi (Canon)
on Aug 04, 2003 at 13:23 UTC ( #280666=note: print w/ replies, xml ) Need Help??

in reply to Perl Idioms Explained - keys %{{map{$_=>1}@list}}

Instead of using `=> 1`, it is valid and cheaper to use `=> undef`.

Comment on Re: Perl Idioms Explained - keys %{{map{$_=>1}@list}}
Replies are listed 'Best First'.
Re: Re: Perl Idioms Explained - keys %{{map{$_=>1}@list}}
by RMGir (Prior) on Aug 04, 2003 at 15:34 UTC
    I'm not disagreeing, but I am curious. Why is it cheaper? One less node allocated? (Oh, I think I see... PL_sv_undef gets reused instead of a new SV?)

    This sounds like a job for those perl internals diagrams someone did at some point... Wish my memory was better.

      Yes. Instead of one scalar of the value '1' for every key, you get to share the same undef value for all the keys and thus don't have to allocate tons of memory you aren't going to use anyway. So keys @{{map { $_, undef } @ary}} has the potential to be a lot cheaper than keys @{{map { $_, 1 } @ary}}. The "a lot" part is just related to how many values are in your array. You burn one extra value for every entry in @ary and I'm just suggesting that you don't have to do that in this case.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://280666]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2015-10-06 23:14 GMT
Find Nodes?
    Voting Booth?

    Does Humor Belong in Programming?

    Results (163 votes), past polls