----- Original Message -----
Sent: Monday, September 12, 2005 1:48 PM
Subject: regexp /o in a sub


Wizards,

Here's one for you. 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). Here's what I'm working on (somewhat abbreviated):

sub Translate
{
my ($row, $val) = @_;

   my $out = "";

   for ($row)
   {
      /^(5|6|23|24|25|26|39|40|41|45|46|51|52|53|54|55|56|60|61|63|69|70|71|72|74|76)$/o && do
      {
         $out = $hashA( $val ) };
         last;
      };
      /^(11|12|13|14|22|28|32|44|50|58|62|64|65|73|75|82|83|85|86|91|92|93)$/o && do
      {
         $out = $hashB{$val};
         last;
      };
      /^(0|2|7|8|9|27|31)$/o && do
      {
         $out = $val;
         last;
      };
      /^(42|43|47|48|49|84)$/o && do
      {
         $out = $hashC{$val};
         last;
      };

      # ...

      /^(57)$/o && do
      {
         $out = $hashP{$val};
         last;
      };

      /^(59)$/o && do
      {
         $out = $hashQ{$val};
         last;
      };

      /^(81)$/o && do
      {
         $out = $hashR{$val};
         last;
      };

   } # end for ($row)

   $out = (length( $out ) > 0) ? $out : "N/A";   # Default value for nulls

   return( $row, $out );

} # sub Translate()

All the sub does is "translate" input to output using different hashes depending on the index passed into the for loop.

There are 18 of these "switch" cases, running from one to 26 OR-ed possibilities in the various cases. BUT, this code is inside a subroutine, so here's the question: does using the /o on these regexps inside a subroutine gain anything in runtime, as opposed to using it in mainline code? What if the script is compiled, using perlapp, into an executable? Is there any gain with /o then (either in mainline code or in a sub) or does any gain go out the window once the code's compiled? If it matters, the sub will get called 94 times per run of the script.

Thanks!
I am not a guru but the algorithm appears to be less than optinum.
 
Of cours if is difficult to truly judge it since the overall schme is not described
 
To me you seem to say that you to:
  use $row to determine which hash to use to obtain  the value from thst hash using the passed $value as a key
 
1.  you are trying to do 2 thing at one time.
 
     Perhaps you should consider creating an array of hashes rather the a set of hashes
 
     Now if the $row matches none of the values in your regex ... 99 then you will loop
through 99 times rather than exiting after one pass
 
Now my approach would be to do it in 2 steps.
 
Use hash where
     the rows are the keys and values are the indexes of the AoH 
 
1   a one-liner can be written to to obtain an index into the AoH
 
2.  a one-liner can be written to use  the index and the $value (really a key) to obtain
    your $out 
 
Occam's Razor says that probably this is the better solotion
 
Use the KISS principle   Keep It Simple, I am Stupid
 
 


Deane

P.S.: Yes, I *do* have way too much time on my hands.... Why else would I be going after such niggling improvements?   ;->
_______________________________________________
ActivePerl mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to