Skip to content

Instantly share code, notes, and snippets.

@cygx
Last active August 29, 2015 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cygx/c1d06377387178097c58 to your computer and use it in GitHub Desktop.
Save cygx/c1d06377387178097c58 to your computer and use it in GitHub Desktop.
# https://raw.githubusercontent.com/kevina/wordlist/master/pos/part-of-speech.txt
my $input = slurp('part-of-speech.txt');
my %words;
sub bench(&code) {
%words = ();
my $start = now;
code;
my $end = now;
say "{ +%words } words parsed in { ($end - $start).round(0.1) }s";
}
bench {
use nqp;
my str $s = $input;
my int $pos = 0;
my int $chars = nqp::chars($s);
while $pos < $chars {
my int $tab = nqp::index($s, "\t", $pos);
my int $nl = nqp::index($s, "\n", $tab);
%words{nqp::p6box_s(nqp::substr($s, $pos, $tab - $pos))} =
nqp::p6box_s(nqp::substr($s, $tab + 1, $nl - $tab - 1));
$pos = $nl + 1;
}
}
bench {
for $input.lines(:eager) {
my $tab = .index("\t");
%words{.substr(0, $tab)} = .substr($tab + 1);
}
}
bench {
for $input.lines(:eager) {
my ($k, $v) = .split("\t", 2);
%words{$k} = $v;
}
}
use Time::HiRes qw(time);
use File::Slurp;
my $input = read_file('part-of-speech.txt');
my %words;
my $start = time;
for ($input =~ /\N+/g) {
my ($k, $v) = split(/\t/, $_, 2);
$words{$k} = $v;
}
my $end = time;
printf "%u words parsed in %.2fs\n", scalar keys(%words), $end - $start;
use nqp;
my %words;
given slurp("part-of-speech.txt") {
my int $pos = 0;
my int $chars = nqp::chars($_);
while $pos < $chars {
my int $tab = nqp::index($_, "\t", $pos);
my int $nl = nqp::index($_, "\n", $tab);
%words{nqp::p6box_s(nqp::substr($_, $pos, $tab - $pos))} =
nqp::p6box_s(nqp::substr($_, $tab + 1, $nl - $tab - 1));
$pos = $nl + 1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment