Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Two simple code style advice questions

by jhourcle (Prior)
on Jan 16, 2013 at 15:54 UTC ( #1013596=note: print w/ replies, xml ) Need Help??


in reply to Two simple code style advice questions

This is probably an over optimization, but for really long lists, I use:

my %ntests = map { $_ => undef } @tests;

And then test using exists. It reduces the memory footprint slightly (or at least, it used to ... I admit I haven't verified that it's still true in more recent versions of perl.)


Comment on Re: Two simple code style advice questions
Select or Download Code
Re^2: Two simple code style advice questions
by parv (Priest) on Jan 16, 2013 at 16:02 UTC

    In that case I am highly partial to...

    my %p; @p{ @q } = ();
      Or even
      my %p; undef @p{ @q } if @q;
      لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re^2: Two simple code style advice questions
by Anonymous Monk on Jan 16, 2013 at 16:02 UTC
    Not sure the point of this. If you are willing to test using exists, then why bother initializing at all? $hash{key}++ is a perfectly valid way to bring a key into existence.
Re^2: Two simple code style advice questions
by choroba (Canon) on Jan 16, 2013 at 16:16 UTC
    Probably not true anymore (v5.10.1):
    $ perl -E 'use Devel::Size qw(total_size); my (%h1, %h2); my @ar = 0 .. 1e6; undef @h1{@ar}; %h2 = map { $_ => undef } @ar; say for map total_size($_), \%h1, \%h2; ' 60083287 60083287
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (6)
As of 2015-07-03 22:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (57 votes), past polls