Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (4)
As of 2024-03-19 11:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found