[Shootout-list] improved spellcheck.perl

Alan Post apost@recalcitrant.org
Thu, 29 Jul 2004 08:22:02 +0000 (UTC)


Three changes:
  Assign undef, rather than 1, into the hash, because perl creates one
    (and only one) SV for undef, instead of creating lots and lots of
    SVs valued 1.

  Test for key presence in hash using exists, instead of truth.  This
    is faster even if we use 1 as the values.

  Use the output record separator.  This doesn't make any difference
    for the sample Input, which produces only one line of output, but
    for larger outputs, you see the difference.

Note also that the program would go faster if we stored the newline in
the hash key, instead of chomping it off.  But that might not fulfill
the requirements for this test.

I was surprised to find that creating the dict hash using a slice was
slower.

*******************

#!/usr/pkg/bin/perl

use strict;

open(DICT, "<Usr.Dict.Words") or
    die "Unable to open Usr.Dict.Words: $_\n";

my %dict;
while (<DICT>) {
    chomp;
    $dict{$_} = undef;
}
close(DICT);

$\ = "\n";
while (<STDIN>) {
    chomp;
    print unless exists $dict{$_};
}