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]; ...
In reply to Ref to hash entries are faster? by Wiggins
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |