http://qs321.pair.com?node_id=462800


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 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.