Hi monks,

Thought this was interesting...

I've a text file that has about 35 000 lines. Every line contains a word that I wanted to replace. I opened the file in NoteTab Light and conveniently used its Replace function to do the replacement. A total of 35 000 replacements were made. It took a while and that got me curious because I'm running Pentium 4 @ 3.20 GHz with 512 MB RAM.

I decided to time the process and found out that it took about 35 seconds - I had no idea how to time it automatically so I used an online stopwatch.

I was naturally curious how fast it would be done in Perl. Amazingly, Perl took less than 2 secs around 1 second. It was hard to believe so I opened the new file to verify the results. Yes, there were indeed 35 000 lines and all the occurences of the word were replaced.

Amazing!

open(FH, "wrongs") or die $!; open(FH2, ">wrongs2") or die $!; while ($line = <FH>) { $line =~ s/wrongs/wrongs3/; print FH2 "$line"; } close (FH); close (FH2);
P.S: If I hadn't done it first in NoteTab Light, I wouldn't have any notion how "fast" Perl's 1 sec is.

Update

Tried doing it in Notepad (the one that comes with Windows) and it hanged!

Update2

Modified my Perl code to count the number of replacements as well as added benchmarking:

use Benchmark; $start = new Benchmark; open(FH, "wrongs") or die $!; open(FH2, ">wrongs2") or die $!; while ($line = <FH>) { $counted = () = $line =~ s/wrongs/wrongs4/; $counted2++ if $counted; print FH2 "$line"; } close (FH); close (FH2); $end = new Benchmark; # calculate difference $diff = timediff($end, $start); print "replaced: $counted2 The operation took: " , timestr($diff, 'all +'); # output # replaced: 35000 The operation took: 0 wallclock secs ( 0.23 usr 0. +03 sys + 0.00 cusr 0.00 csys = 0.27 CPU)
Is there a better way to count the total number of replacements?


In reply to Word replace - notetab light vs perl by kiat

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.