Dear monks, I have written a simple Perl code to do some text processing but I'm sure it may be optimized. The tast is to process a text file, delete garbage from it and count the words inside (using hash), and then print out the list of the words alphabetically with their numbers on the second column. By garbage I mean strings which include numbers like: 345asfg435t, 345gf, er345, etc.

So here is the code (some characters in regex patt. may not display correct in your browsers because they are Turkish charachers, oops! they are converted to entities, anyway):
#! /usr/bin/perl while (<STDIN>) { s/(\d+)?([a-zA-Z&#305;ü&#287;&#351;çö&#304;&#350;Ö&#286;Ü]+\d+)+|( +\d+[a-zA-Z&#305;ü&#287;&#351;çö&#304;&#350;Ö&#286;Ü]+)//og; $MyString = $MyString . $_; } while ($MyString =~ /([a-zA-Z&#305;ü&#287;&#351;çö&#304;&#350;Ö&#286;Ü +]+)/og) { $temp = $1; $temp =~ tr/[A-Z]/[a-z]/; $MyWordCount{$temp}++; } print "Word\t\t\tFrequency\n"; print "======\t\t\t===========\n"; #sorting alphabetically foreach $key (sort keys %MyWordCount) { if (length($key) > 7) { print "$key\t\t$MyWordCount{$key}\n"; } else { print "$key\t\t\t$MyWordCount{$key}\n"; } }


This code is run on Win2K ActiveState perl version v5.8 in such a fashion:

perl csharpversusperl.pl<input.txt >output.txt

I'm comparing it with C# programs (done for an assignment) and C# programs also use command line redirection, not some system file processing methods. The current situation is that this Perl code takes 10 seconds to process a 750K file but one of the C# code (not very optimized though) takes about 3-4 seconds.

I'd like to learn if this code could be much faster, if I'm doing something wrong. Please enlighten me.

In reply to How much can this text processing be optimized? by YAFZ

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.