This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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; | |
| } | |
| } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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