Steve,
This looked promising, but when I coded a foo-file to try it, I discovered that the %actions hash must be declared after the values of $row and $val are known--that is, inside the Translate sub itself. More fiddling is required, but I wonder how much time would be taken by creating that 94-member hash 94 times as compared to evaluating the switch. Another problem: five of the cases elided in my original email involve executed code, not just grabbing a hash-member. Any thoughts, suggestions, or advice?
Thanks. It's an intriguing suggestion of another way of looking at the problem. I'm going to play with it some more...
Deane
| Steve Dawson <[EMAIL PROTECTED]>
Sent by: [EMAIL PROTECTED] 09/12/2005 18:53
|
To: [email protected] cc: Subject: Re: regexp /o in a sub |
Quoting "[EMAIL PROTECTED]" <[EMAIL PROTECTED]>:
>I'm doing some optimizing on a script (using timethese
> and cmpthese calls in stub programs) and I was wondering about this. I
> know the compile-once option on regular expressions has been maligned, but
> I've found that it gets me about a 3% increase on one piece of code that
> uses a for loop with regexps as a pseudo-switch statement (just like in
> the Camel book).
Can I ask a question?, Would it be faster to eliminate all of the regex
matches and forget about the half poofteenth per transaction saving from /o
and use the incoming value of $line as an index to a hash of accepted values?
something like this:
#pre-declare a hash of your actions outside where you declare %hashA, %hashB
etc. You could possibly use refs to anonymous subs as hash values to do more
complex things and tidy up duplication...
%actions = ( 0 => $val,
1 => $hashC{$val},
...
51 => $hashA{$val}
);
sub Translate{
my ($row,$val) = @_;
my $out = $actions{$row} || 'N/A';
return( $row, $out );
}
_______________________________________________
ActivePerl mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
_______________________________________________ ActivePerl mailing list [email protected] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
