When I read this, I thought "Don't be ridiculous! Yours is probably the most efficient method suggested!" Then I set about benchmarking the various suggestions to prove it. Alas, yours is only the second most efficient (in terms of time):
Rate inman blazar2 blazar1 jeanluca thedoe2 prasadbabu drmoron salva thedoe1
inman 107580/s -- -7% -28% -33% -51% -54% -72% -77% -78%
blazar2 115313/s 7% -- -23% -28% -48% -51% -70% -75% -76%
blazar1 149209/s 39% 29% -- -7% -32% -37% -61% -68% -69%
jeanluca 160157/s 49% 39% 7% -- -27% -32% -58% -65% -67%
thedoe2 220552/s 105% 91% 48% 38% -- -6% -43% -52% -54%
prasadbabu 235856/s 119% 105% 58% 47% 7% -- -39% -49% -51%
drmoron 384090/s 257% 233% 157% 140% 74% 63% -- -16% -20%
salva 459627/s 327% 299% 208% 187% 108% 95% 20% -- -5%
thedoe1 481345/s 347% 317% 223% 201% 118% 104% 25% 5% --
| [reply] [d/l] [select] |
I can see NO reason why thedoe1 should be performing any better than salva's code.
salva => sub { (my $dum) = $str =~ /^.*abc\s(\d+)/s; },
thedoe1 => sub { (my $dum) = $str =~ /(?<!abc).*abc\s(\d+)/gs; },
Those regexes are equivalent. In fact, I can't understand why in the world thedoe used a look-behind there. It accomplishes nothing, since the first place the regex tries to match is at the beginning of the string. The only difference that it could make is if pos($str) is something other than 0, and then that means it would not necessary operate properly (insofaras what was requested from the regex). Sorry to sound grumpy, but this is a misuse of a look-behind (and the /g modifier) that I think should be pointed out. There's no voodoo going on.
| [reply] [d/l] [select] |
well, you are benchmarking a corner case, that's when the last abc \d+ is at the end of the string.
When I said that I didn't know about its performance I was thinking on some not so convenient case, for instance, if the matching substr is near the beginning, in that case perl regexp engine is going to backtrack a lot and maybe looping with the OP regexp and discarding all but the last match could perform better.
| [reply] [d/l] |