I wrote a little program that runs a million string replace operations using the three methods that have been suggested here so far, and it displays how many seconds it takes to run each replace operation. (I am using tinyperl 5.8 on Windows XP Pro SP2. And I am a beginner Perl programmer.)

As it turns out, if the length of the resulting string is longer than the original after the replacement, then it takes longer to do the replacement. Btw, the regexp has been the fastest method. The split/join method was average. And the substr method was the slowest! It was so slow that I had to abort the program after it replaced 1 million "HelloWorld" strings to "XXWorld." It took more than an hour! Anyway, here is the code:

This program will perform 1000000 string replace operations and print the number of seconds it takes to complete each. Testing string replace method #1. Replace 'H' --> 'X' 0 second(s) Replace 'Hello' --> 'XX' 0 second(s) Replace 'Hello' --> '' 1 second(s) Replace 'Hel' --> 'XXXXXX' 14 second(s) Replace 'HelloWorld' --> 'XXXXXXXXXXXXX' 14 second(s) Testing string replace method #2. Replace 'H' --> 'X' 5 second(s) Replace 'Hello' --> 'XX' 2 second(s) Replace 'Hello' --> '' 1 second(s) Replace 'Hel' --> 'XXXXXX' 16 second(s) Replace 'HelloWorld' --> 'XXXXXXXXXXXXX' 1 second(s) Testing string replace method #3. Replace 'H' --> 'X' 1 second(s) Replace 'Hello' --> 'XX' 4928 second(s) Replace 'Hello' --> '' ################################################## #PROGRAM STARTS HERE: use strict; use warnings; # See: # http://www.perlmonks.org/?node_id=98357 my $T; my $str; my $find = ""; my $replace = ""; my $REPEAT = 1000000; my $METHOD = 1; print "This program will perform $REPEAT string replace operations\n"; print "and print the number of seconds it takes to complete each.\n"; # RUN TESTS for ($METHOD = 1; $METHOD <= 3; $METHOD++) { print "\nTesting string replace method #$METHOD.\n"; RunTest("H", "X"); RunTest("Hello", "XX"); RunTest("Hello", ""); RunTest("Hel", "XXXXXX"); RunTest("HelloWorld", "XXXXXXXXXXXXX"); } ##################################### sub RunTest { if (@_ != 2) {return;} ($find, $replace) = @_; $| = 1; # Disable buffering $str = 'HelloWorld' x $REPEAT; my $PADDING1 = ' ' x (13 - length($find)); my $PADDING2 = ' ' x (13 - length($replace)); print "Replace '$find'$PADDING1--> '$replace'$PADDING2"; # This method was suggested by tachyon. if ($METHOD == 1) { $find = quotemeta $find; # escape regex metachars if present $T = time; # measure time at the start. $str =~ s/$find/$replace/g; printTime(); # measure time at the end. } # This method was suggested by harangzsolt33 if ($METHOD == 2) { $T = time; # measure time at the start. $str = join( $replace, split($find, $str) ); printTime(); # measure time at the end. } # This method was suggested by davorg. if ($METHOD == 3) { $T = time; # measure time at the start. my $pos = index($str, $find); while ( $pos > -1 ) { substr( $str, $pos, length( $find ), $replace ); $pos = index( $str, $find, $pos + length( $replace )); } printTime(); # measure time at the end. } return; } ########################### sub printTime { my $DIFF = (time - $T); my $PADDING = ' ' x (3 - length($DIFF)); print "$PADDING $DIFF second(s)\n"; } ###########################

In reply to Re: How do I replace a substring (if exists) with a different substring in a string? by harangzsolt33
in thread How do I replace a substring (if exists) with a different substring in a string? by kommesel

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.