Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: How To Do This Better?

by ahunter (Monk)
on Apr 14, 2000 at 23:07 UTC ( #7653=note: print w/ replies, xml ) Need Help??


in reply to How To Do This Better?

Well, one of the great things about Perl is that there's always a way of cheating. In this case, the cheat is to note that s/// returns the number of replacements (and gets rid of the letters we've already looked at). So:

{ local $/ = undef; # Read everything in one go $_ = lc(<STDIN>); s/[^a-z]//g; # Get rid of nonalphabetic character +s for my $letter ('a'..'z') { my $count = s/$letter//g; # Magic! print "$letter = $count\n"; } }
Now, that's many times faster than your original, and a fair bit shorter. I suspect it can be made faster still, though (is the slowdown produced by using s/// offset by the speedup on the next iteration, for instance?)

perl /home/ahunter/original.pl 154.02s user 2.39s system 88% cpu 2:56.29 total
perl /home/ahunter/flib.pl < xlib.ps 4.48s user 0.14s system 93% cpu 4.942 total

Well, that *was* impressive...
-- Andrew


Comment on Re: How To Do This Better?
Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://7653]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (13)
As of 2014-09-18 19:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (121 votes), past polls