Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Minimum Hash keys allocated?

by afoken (Chancellor)
on Sep 26, 2010 at 18:20 UTC ( [id://862104]=note: print w/replies, xml ) Need Help??


in reply to Minimum Hash keys allocated?

Premature optimization is the root of all evil. Just don't do that. Perl will handle allocation good enough most of the times. There may be rare cases where preallocation may be useful, but then, you would use Devel::NYTProf or similar first to find and eliminate other bottlenecks.

To save keystrokes, use my %hash=(); or just my %hash;, both are technically equal. Some people (including me) prefer the first form, refusing to rely on the implementation details of Perl to initialize the variable with an empty hash / empty array / undefined scalar, other people think that the documentation is very clear on this fact and the initializing assignment is just too much and redundant work.

Alexander

--
Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

Replies are listed 'Best First'.
Re^2: Minimum Hash keys allocated?
by halfcountplus (Hermit) on Sep 26, 2010 at 18:50 UTC

    In fact, pre-allocating can save some headaches if you know the data structure is going to be a very large one, qv:

    mem usage

    Pay close attention to what ikegami says in this part:

    Re^4: mem usage

    As this sort of hack made a very real difference -- it is not simply a matter of setting the number of slots.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (7)
As of 2024-04-19 08:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found