Wiggins has asked for the wisdom of the Perl Monks concerning the following question:
Old habits dye hard, or at least dark grey.
Coming from a 'C'/C++ background, I hate to constantly access an array element in a loop using the array subscripting. I prefer a pointer to the entry after computing subscripts once.
With a hash, is it better to take a reference to the the keyed scalar, or repeatedly recompute the hash?
A snippit of a 60 line loop
Does 'locality or reference' optimized that hash access, or is the key rehashed on every use in the loop?ELEMENT: foreach $key (keys %Contracts) { $tot_count++; {# lock block lock @{$Contracts{$key}}; next ELEMENT if ($Contracts{$key}[STATE] eq $ST_VOID); next ELEMENT if ($Contracts{$key}[STATE] eq $ST_REPORTED +); $elapsed_time = time() - $Contracts{$key}[START]; .... lots more accesses to the $Contracts{$key} record.
?Better?
Wisdom is needed, pleaseELEMENT: foreach $key (keys %Contracts) { $tot_count++; {# lock block lock @{$Contracts{$key}}; $Cntrct_ref = \$Contracts{$key}; next ELEMENT if ($Cntrct_ref[STATE] eq $ST_VOID); next ELEMENT if ($Cntrct_ref[STATE] eq $ST_REPORTED); $elapsed_time = time() - $Cntrct_ref[START]; ...
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Ref to hash entries are faster?
by Tanktalus (Canon) on Nov 29, 2006 at 20:29 UTC | |
by shotgunefx (Parson) on Nov 29, 2006 at 20:36 UTC | |
Re: Ref to hash entries are faster?
by GrandFather (Saint) on Nov 29, 2006 at 21:12 UTC | |
by ikegami (Patriarch) on Nov 29, 2006 at 23:29 UTC | |
by GrandFather (Saint) on Nov 30, 2006 at 00:06 UTC | |
by Wiggins (Hermit) on Nov 29, 2006 at 21:28 UTC | |
by Not_a_Number (Prior) on Nov 29, 2006 at 23:16 UTC | |
Re: Ref to hash entries are faster?
by shotgunefx (Parson) on Nov 29, 2006 at 20:18 UTC | |
Re: Ref to hash entries are faster?
by fenLisesi (Priest) on Nov 30, 2006 at 08:05 UTC |
Back to
Seekers of Perl Wisdom