[Melbourne-pm] Regular expression musings

Jacinta Richardson jarich at perltraining.com.au
Wed Jun 4 22:55:42 PDT 2008


Alfie John wrote:
> Hey Jacinta,
> 
> If you're talking about optimisations, I think it would also be a good
> idea to include the tip - "don't use regular expressions". Using the
> same samples, I would be interested in seeing the results of a
> index()/substr() combo.

Oh it will be in there.  ;)

Here are your requested results:


Comparing over a string of length: 2159

Whole string quoted
               Rate  brackets1    substr1 dot_quest1  dot_star1
brackets1   15420/s         --       -85%       -85%       -93%
substr1    101824/s       560%         --        -0%       -53%
dot_quest1 102167/s       563%         0%         --       -53%
dot_star1  218741/s      1319%       115%       114%         --

First word only quoted
               Rate  dot_star2    substr2  brackets2 dot_quest2
dot_star2   46839/s         --       -91%       -94%       -94%
substr2    545553/s      1065%         --       -26%       -34%
brackets2  735254/s      1470%        35%         --       -11%
dot_quest2 822619/s      1656%        51%        12%         --

Last word only quoted
               Rate  brackets3 dot_quest3  dot_star3    substr3
brackets3   66313/s         --        -1%        -2%       -44%
dot_quest3  67200/s         1%         --        -0%       -43%
dot_star3   67411/s         2%         0%         --       -43%
substr3    117791/s        78%        75%        75%         --

Single starting quote
               Rate    substr4 dot_quest4  brackets4  dot_star4
substr4    178739/s         --        -7%        -8%        -8%
dot_quest4 191837/s         7%         --        -1%        -2%
brackets4  194053/s         9%         1%         --        -0%
dot_star4  194858/s         9%         2%         0%         --


Using:

        substr4 => sub {
                my $i1 = index($string4, q{"}, 0);
                return if $i1 < 0;
                my $i2 = index($string4, q{"}, $i1+1);
                return if $i2 < 0;
                my $str = substr($string4, $i1, $i2-$i1);
                return $str;
        }

I'm a little surprised that substr does so badly on the single starting quote,
but there you have it.

All the best,

	J

-- 
   ("`-''-/").___..--''"`-._          |  Jacinta Richardson         |
    `6_ 6  )   `-.  (     ).`-.__.`)  |  Perl Training Australia    |
    (_Y_.)'  ._   )  `._ `. ``-..-'   |      +61 3 9354 6001        |
  _..`--'_..-_/  /--'_.' ,'           | contact at perltraining.com.au |
 (il),-''  (li),'  ((!.-'             |   www.perltraining.com.au   |


More information about the Melbourne-pm mailing list