Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use v6;
- my $N = 100_000;
- my %words;
- print "generating input...";
- my $input = join '', map { "$_\t42\n" }, 1..$N;
- print "done.\n";
- sub bench($name, &code) {
- %words = ();
- my $start = now;
- code;
- my $end = now;
- say "{ +%words } words parsed in { ($end - $start).round(0.01) }s ($name)";
- }
- bench 'global match', {
- %words = ($input ~~ m:g/(<-[\n\t]>+)\t(\N+)/).map(*>>.Str.Slip);
- }
- bench 'split on regex', {
- for $input.lines {
- my ($k, $v) = .split(/\t/, 2);
- %words{$k} = $v;
- }
- }
- bench 'split on string', {
- for $input.lines {
- my ($k, $v) = .split("\t", 2);
- %words{$k} = $v;
- }
- }
- bench 'index', {
- for $input.lines {
- my $tab = .index("\t");
- %words{.substr(0, $tab)} = .substr($tab + 1);
- }
- }
- bench 'FORTRAN', {
- my str $str = $input;
- my int $pos = 0;
- my int $chars = $str.chars;
- while $pos < $chars {
- my int $tab = $str.index("\t", $pos);
- my int $nl = $str.index("\n", $tab);
- %words{$str.substr($pos, $tab - $pos)} =
- $str.substr($tab + 1, $nl - $tab - 1);
- $pos = $nl + 1;
- }
- }
- bench 'FORTRAN using NQP builtins', {
- use nqp;
- my str $str = $input;
- my int $pos = 0;
- my int $chars = nqp::chars($str);
- while $pos < $chars {
- my int $tab = nqp::index($str, "\t", $pos);
- my int $nl = nqp::index($str, "\n", $tab);
- %words{nqp::p6box_s(nqp::substr($str, $pos, $tab - $pos))} =
- nqp::p6box_s(nqp::substr($str, $tab + 1, $nl - $tab - 1));
- $pos = $nl + 1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement