You probably meant to say:
while(!eof(FILE))
{
my $c = getc(FILE);
$count{lc($c)}++ if $c=~/[a-zA-Z]/;
}
As your original seems to give up at the first newline.
Plus, you have to remember that perl compiles regular
expressions to make them run faster, particularily when
they don't require backtracking (basically creates a
finite state machine to do the job). As these are executed
in C, writing perl to do the same job is *always* going
to be slower.
Plus getc() isn't exactly a star performer, either. Maybe
use the unbuffered IO stuff if you want to improve
performance in this area, though you'd really have to
be after squeezing the last ounce of speed out of the
thing in that case (and you'd have to remember not ever to
use any of the buffered routines)
The important thing is to try it, of course, especially
where perl performance is concerned. Remember that perl
is interpreted (it compiles to a byte-code at runtime), but
the internal functions are compiled, and are always faster.
perl /home/ahunter/grob.pl < xlib.ps 39.04s user 0.24s system 95% cpu 41.126 total
Well, a 3x speed-up over the original isn't really all
that bad, I suppose.
-- Andrew
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.