Try this:
#! perl -slw
use strict;
use threads;
my @words = do{ local( @ARGV, $/ ) = 'your.dictionary'; <> };
chomp @words;
my %dict;
undef @dict{ @words };
my $errors = 0;
my $savedfh;
my @files = glob "*";
open $savedfh, '<', shift( @files ) or die $!;
for my $file ( @files ) {
my $fh = $savedfh;
my $thread = async{ open my $fh, '<', $file or die $!; $fh };
map +( exists $dict{$_} || ++$errors ), split while <$fh>;
$savedfh = $thread->join;
}
print $errors;
There are three attempted optimisations going on here:
- By amalgamating the reading from a file; splitting into words; looking up the hash; and counting; into a single line, it avoids two extra levels of scope and the building of the array.
- Using my variable which are quicker than globals.
It also allows strict & warnings which I prefer.
- Using threads to overlap the opening of the next file whilst processing the last.
This is probably where most of your time is going. The initial lookup of a file, especially if it is in a large directory is often quite expensive in terms of time.
This will only be effective if you are processing a few large files rather than zillions of small ones.
Try it and see what if any benefit you derive. If it is effective and you want to understand more, just ask.
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
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.