Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
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.)

Replies are listed 'Best First'.
Re^2: Two simple code style advice questions
by choroba (Bishop) 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
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re^2: Two simple code style advice questions
by parv (Vicar) 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.

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 drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2019-08-19 11:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    If you were the first to set foot on the Moon, what would be your epigram?






    Results (140 votes). Check out past polls.

    Notices?