Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Want a faster replace, Please

by SFLEX (Chaplain)
on Sep 12, 2010 at 12:45 UTC ( [id://859841]=perlquestion: print w/replies, xml ) Need Help??

SFLEX has asked for the wisdom of the Perl Monks concerning the following question:

I have been working on my module to speed it up and have gained a lot of speed, but I see there are still some spots I can gain a lot more.

This is the code I want to speed up, I would like to have it run in a loop but those loops seems to like to eat time.
my @AUBBC_TAGS = ('b','i','sub','sup','pre','ul','ol','small','big','l +i','p','h1','h2','h3','h4','h5','h6','div','strong'); my $message = '[b] stuff [i] stuff [/i] [/b]'; foreach (@AUBBC_TAGS) { $message =~ s{\[(.?)?$_\]}{<$1$_>}xg; } print $message;

I know this will leave open tags in HTML and I dont realy care. Speed is what Im after here.

The code that I made that was faster is.

my $message = '[b] stuff [i] stuff [/i] [/b]'; $message =~ s{\[(.?)?([\w]+)\]}{<$1$2>}xgo; print $message;

But that code doesn't work off a list and I would like if it did.

Update 2: Everyones post helped so ++ to all.
Since the array was so small I ended up just doing this.
$message =~ s/\[(\/?(?:big|h[123456]|[ou]?li?|pre|s(?:mall|trong|u[bp])|[bip]))\]/<$1>/gxo;
I guess I will stick with that for now.

Replies are listed 'Best First'.
Re: Want a faster replace, Please
by Kanji (Parson) on Sep 12, 2010 at 13:12 UTC
    my $AUBBC_TAGS = join '|', @AUBBC_TAGS; my $message = '[b] stuff [i] stuff [/i] [/b]'; $message =~ s{\[(.?)?($AUBBC_TAGS)\]}{<$1$2>}xgo; # or s{\[(/?(?:$AUBBC_TAGS))\]}{<$1>}xgo; print $message;

        --k.


Re: Want a faster replace, Please
by Khen1950fx (Canon) on Sep 12, 2010 at 13:17 UTC
    Is this any better?
    #!/usr/bin/perl use strict; use warnings; my @AUBBC_TAGS = ( 'b', 'i', 'sub', 'sup', 'pre', 'ul', 'ol', 'small', 'big', 'li', 'p', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'div', 'strong' ); my $message = '[b] stuff [i] stuff [/i] [/b]'; foreach (@AUBBC_TAGS) { $message =~ s/\[(.?)?$_\]/<$1$_>/gx; } print $message, "\n";
      I've looked really hard, but what it the difference between your suggestion and the code the OP posted?
Re: Want a faster replace, Please
by jwkrahn (Abbot) on Sep 12, 2010 at 14:59 UTC

    Change:

    foreach (@AUBBC_TAGS) { $message =~ s{\[(.?)?$_\]}{<$1$_>}xg; }

    To:

    $message =~ s{\[(.?)$_\]}{<$1$_>}g foreach @AUBBC_TAGS;
Re: Want a faster replace, Please
by JavaFan (Canon) on Sep 12, 2010 at 18:11 UTC
    Note that your regexp is wrong, it will replace [li] with <i>, as "li" matches /.?i/.

    Of course, if it's speed you're after, and not correctness, you maybe able to get away with:

    $message =~ y/[]/<>/;
Re: Want a faster replace, Please
by Anonymous Monk on Sep 12, 2010 at 20:47 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://859841]
Approved by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (7)
As of 2024-04-19 07:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found