in reply to Should we bother to save hash lookups for performance?

For a single level of indirection, as shown in your example, no. (And if you do, don't use the variables $a and $b, leave them for sort comparison functions).

On the other hand, for deeply nested hashes, yes, absolutely, assuming you're going to refer to the variable two or more times. It will make your code much more compact and easier to read.

my $bytes = $self->{center}{floor}{room}{bay}{rack}{unit}{port}{tran +smitted}; if( $bytes == 0 ) { print "Nothing transmitted.\n"; } elsif( $max < $bytes ) { $max = $bytes; } else { $sum += $bytes; } }

Keep in mind that I'm not doing this for performance reasons, I'm only concerned about readability.


print@_{sort keys %_},$/if%_=split//,'= & *a?b:e\f/h^h!j+n,o@o;r$s-t%t#u'
  • Comment on Re: Should we bother to save hash lookups for performance? (yes, sometimes)
  • Download Code

Replies are listed 'Best First'.
Re: Re: Should we bother to save hash lookups for performance? (yes, sometimes)
by BrowserUk (Patriarch) on Oct 19, 2002 at 20:15 UTC

    This becomes especially useful when there exists and you need to access several things at a given level.

    my $port = $self->{center}{floor}{room}{bay}{rack}{unit}{port}; unless ( --$port->{timecount} ) { unless ( $port->{transmitted} or $port->{received} ) { close( $port->{handle} ); } else { $port->{timecount} = $port->{timeout}; $port->{transmitted} = $port->{recieved} = 0; } }

    This neatly emulates the Pascal-style with ... statement.

    I guess you could even use my $with_port = ... but that would be altogether to cute (and extra to type:^).


    Cor! Like yer ring! ... HALO dammit! ... 'Ave it yer way! Hal-lo, Mister la-de-da. ... Like yer ring!
      for($self->{center}{floor}{room}{bay}{rack}{unit}{port}) { last if --$_->{timecount}; close($_->{handle}), last unless $_->{transmitted} or $_->{received} $_->{timecount} = $_->{timeout}; $_->{transmitted} = $_->{recieved} = 0; }

      Makeshifts last the longest.

        ...BUT, BUT, BUT...it was only pseudo-code.... Aristotle++

        You can tell its a slow weekend at PM when you start optomisiing my pseudo-code:)


        Cor! Like yer ring! ... HALO dammit! ... 'Ave it yer way! Hal-lo, Mister la-de-da. ... Like yer ring!