Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Re: Minimum Hash keys allocated?

by afoken (Canon)
on Sep 26, 2010 at 18:20 UTC ( #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.


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?

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (9)
As of 2020-08-05 11:14 GMT
Find Nodes?
    Voting Booth?
    Which rocket would you take to Mars?

    Results (35 votes). Check out past polls.