in reply to Which, if any, is faster?

Array lookup is faster than hash lookup, but the time you save is totally swamped by the time Perl takes to indirect through a blessed reference. If you shift your args rather than using the aliasing of @_ you waste a little more and if you use accessors, you'll slow it down (a lot ) more.

Basically, if speed is truely a criteria, don't use OO in Perl.


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.

Replies are listed 'Best First'.
Re^2: Which, if any, is faster?
by monarch (Priest) on Jun 01, 2005 at 13:32 UTC
    Personally I like to stay clear of hashes of hashes, particularly where the second hash contains common keys (e.g. the column names of a table).

    At the end of the day arrays are probably not that much faster than hashes.. but hashes sure do take up a lot of extra memory with those verbose keys.. particularly if the verbose keys are containing redundant information.

      I like HOHes. At times they have contributed significantly to my WUD (What U Did) when they were the right thing to use.

      If this isn't always the fastest solution? Well, I code for understandability and maintainability first. Speed... enh. :)

      ... arrays are probably not that much faster than hashes ...

      Indeed not. Instead of switching from arrays to hashes you can probably get a similar speed-up by getting some slightly faster hardware — which'll likely be cheaper than what it'd cost in extra development time to have to deal with inappropriate data structures.

      ... but hashes sure do take up a lot of extra memory with those verbose keys.. particularly if the verbose keys are containing redundant information.

      That simply isn't true. Wanting to have several separate hashes with the same keys is a common situation. Indeed it's one the Perl 5 Porters considered, so they explicitly coded to optimize the memory usage: all hash keys are only stored in memory once no matter no many hashes they are used in -- see perl5004delta.

      To summarise: use hashes where each key contains some unique information, and use arrays where key names would be duplicated under a hash arrangement.

      To summarize: don't try to second-guess Perl, and don't assume that you are cleverer that the Perl 5 Porters.

      Smylers

        I read the following as array-based objects are 20% faster and 70% 40% smaller than the hash based equivalent for little extra effort and no loss of clarity.

        #! perl -slw use strict; package ArrayBased; use constant { CLASS => 0, SELF => 0, FIRST => 0, SECOND => 1, THIRD => 2, FOURTH => 3, FIFTH => 5, }; sub new { return bless [ qw[ one two three four five ] ], $_[CLASS]; } sub method { (undef) = $_[SELF][FIRST]; (undef) = $_[SELF][SECOND]; (undef) = $_[SELF][THIRD]; (undef) = $_[SELF][FOURTH]; (undef) = $_[SELF][FIFTH]; $_[SELF][FIRST] = 1; $_[SELF][SECOND]= 2; $_[SELF][THIRD] = 3; $_[SELF][FOURTH]= 4; $_[SELF][FIFTH] = 5; } package HashBased; sub new { my( $class ) = @_; return bless { FIRST => 'one', SECOND => 'two', THIRD => 'three', FOURTH => 'four', FIFTH => 'five', }, $class; } sub method { my( $self ) = @_; (undef) = $self->{FIRST}; (undef) = $self->{SECOND}; (undef) = $self->{THIRD}; (undef) = $self->{FOURTH}; (undef) = $self->{FIFTH}; $self->{FIRST} = 1; $self->{SECOND} = 2; $self->{THIRD} = 3; $self->{FOURTH} = 4; $self->{FIFTH} = 5; } package main; use Benchmark qw[ cmpthese ]; use Devel::Size qw[ size total_size ]; cmpthese -1, { arrayBased => q[ my $o = new ArrayBased; $o->method; ], hashBased => q[ my $o = new HashBased; $o->method; ], }; print "ArrayBased bytes: ", total_size( ArrayBased->new() ); print " HashBased bytes: ", total_size( HashBased->new() ); __END__ P:\test>462336 Rate hashBased arrayBased hashBased 48505/s -- -16% arrayBased 57853/s 19% -- ArrayBased bytes: 220 HashBased bytes: 373

        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.