Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Re: Optimizing a regex that replaces near the end of a string

by thunders (Priest)
on Mar 18, 2009 at 21:31 UTC ( #751578=note: print w/replies, xml ) Need Help??

in reply to Optimizing a regex that replaces near the end of a string

If your end string is really something static like that, you may not want to use a regex at all. Here's a case insensitive replacement using rindex and substr:
use Benchmark qw(cmpthese); fbard@devo1:~/spot_camp_layout$ cat rindex use Benchmark qw(cmpthese); my $x = ("x" x 100_000)."</body></html>"; my $lc_x = lc($x); my $end_html = qr{(</body></html>)}; cmpthese(100_000,{ rindex_substr => sub{ my $str = $x; substr($str,rindex($lc_x,"</body>"),0,"yyyy"); }, regex => sub{ my $str = $x; $str =~ s|$end_html$|yyyy$1|i; } }); Rate regex rindex_substr regex 18416/s -- -75% rindex_substr 73529/s 299% --

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://751578]
[marioroy]: LA only $ and \ needed backslash, not ' and ".
[marioroy]: my @music_times = qx(find \$HOME/Music/ Albums -type f -iname '*.mp3' -exec mp3info -p "%S\\n" {} \\;);
[marioroy]: I wanted to come back and report on that.
[marioroy]: cheers

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (7)
As of 2017-04-23 22:38 GMT
Find Nodes?
    Voting Booth?
    I'm a fool:

    Results (433 votes). Check out past polls.