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 Benchmark.pm 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 constant.pm 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.


In reply to Re: Which, if any, is faster? by schwern
in thread Which, if any, is faster? by rvosa

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.