Advertisement
Guest User

Untitled

a guest
Oct 24th, 2016
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.54 KB | None | 0 0
  1. use v6;
  2.  
  3. my $N = 100_000;
  4. my %words;
  5.  
  6. print "generating input...";
  7. my $input = join '', map { "$_\t42\n" }, 1..$N;
  8. print "done.\n";
  9.  
  10. sub bench($name, &code) {
  11. %words = ();
  12.  
  13. my $start = now;
  14. code;
  15. my $end = now;
  16.  
  17. say "{ +%words } words parsed in { ($end - $start).round(0.01) }s ($name)";
  18. }
  19.  
  20. bench 'global match', {
  21. %words = ($input ~~ m:g/(<-[\n\t]>+)\t(\N+)/).map(*>>.Str.Slip);
  22. }
  23.  
  24. bench 'split on regex', {
  25. for $input.lines {
  26. my ($k, $v) = .split(/\t/, 2);
  27. %words{$k} = $v;
  28. }
  29. }
  30.  
  31. bench 'split on string', {
  32. for $input.lines {
  33. my ($k, $v) = .split("\t", 2);
  34. %words{$k} = $v;
  35. }
  36. }
  37.  
  38. bench 'index', {
  39. for $input.lines {
  40. my $tab = .index("\t");
  41. %words{.substr(0, $tab)} = .substr($tab + 1);
  42. }
  43. }
  44.  
  45. bench 'FORTRAN', {
  46. my str $str = $input;
  47. my int $pos = 0;
  48. my int $chars = $str.chars;
  49. while $pos < $chars {
  50. my int $tab = $str.index("\t", $pos);
  51. my int $nl = $str.index("\n", $tab);
  52. %words{$str.substr($pos, $tab - $pos)} =
  53. $str.substr($tab + 1, $nl - $tab - 1);
  54. $pos = $nl + 1;
  55. }
  56. }
  57.  
  58. bench 'FORTRAN using NQP builtins', {
  59. use nqp;
  60. my str $str = $input;
  61. my int $pos = 0;
  62. my int $chars = nqp::chars($str);
  63. while $pos < $chars {
  64. my int $tab = nqp::index($str, "\t", $pos);
  65. my int $nl = nqp::index($str, "\n", $tab);
  66. %words{nqp::p6box_s(nqp::substr($str, $pos, $tab - $pos))} =
  67. nqp::p6box_s(nqp::substr($str, $tab + 1, $nl - $tab - 1));
  68. $pos = $nl + 1;
  69. }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement