Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

Re: Which, if any, is faster?

by schwern (Scribe)
on Jun 02, 2005 at 07:58 UTC ( #462800=note: print w/replies, xml ) Need Help??

in reply to Which, if any, is faster?

Will the data from $b->[0] be returned faster than that from $a->{'High'}?

This changes from Perl version to Perl version, compiler to compiler, CPU to CPU, etc... It also varies if $a/$b is lexical or global and if the index/key is a static string/number vs a variable. In any case, the difference is almost neglidgable. Perl's hashes are surprisingly fast (or, if you're a pessimist, Perl's arrays are surprisingly slow). You can benchmark it with if you really want but realize your benchmarks are likely to change in the next version of Perl or machine you run the code on.

What if I write proper accessors?

Then the cost of calling the accessor function will completely swamp any difference in hash or array access. If you do go with the array technique, please be kind to your future maintainers and use constants to access the arrays rather than raw numbers.

use constant LEFT => 0; use constant RIGHT => 1; $self->[LEFT] = $params{Left}; $self->[RIGHT] = $params{Right};

However, the cost of loading will probably eat more CPU time than what you might save using array objects. You could define the constants manually using sub LEFT () { 0 } but you see how this is rapidly getting complicated.

Which is to say, just use hashes. They're simpler and when all the smoke clears your code will be just as fast.

Replies are listed 'Best First'.
Re^2: Which, if any, is faster?
by BrowserUk (Patriarch) on Jun 02, 2005 at 08:31 UTC
    However, the cost of loading will probably eat more CPU time than what you might save using array objects.

    That depends entirely upon how many times you access your objects. Loading is a one-time hit at startup. The savings of array-based access acrue. The more your program does with your array-based instances, and the longer it runs, the greater the savings.

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://462800]
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: (7)
As of 2022-01-21 09:07 GMT
Find Nodes?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:

    Results (57 votes). Check out past polls.