DragonHawk

qr-re-time-trial.pl

Sep 13th, 2022
1,276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 0.75 KB | Source Code | 0 0
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5. use English;
  6. use feature qw( say );
  7. use autodie qw( :all );
  8. use Time::HiRes qw(gettimeofday tv_interval);
  9.  
  10. open(my $fh, "<", "/usr/share/dict/words");
  11. my @words = <$fh>;
  12. close($fh);
  13.  
  14. my $start;
  15. my $count;
  16. my $str = 'b.+ean';
  17. my $re = qr/$str/;
  18.  
  19. $count = 0;
  20. $start = [gettimeofday];
  21. foreach (@words) {
  22.     $count++ if /$re/;
  23.     }
  24. say "precompile : count=$count interval=" . tv_interval($start);
  25.  
  26. $count = 0;
  27. $start = [gettimeofday];
  28. foreach (@words) {
  29.     $count++ if /b.+ean/;
  30.     }
  31. say "hardcode   : count=$count interval=" . tv_interval($start);
  32.  
  33. $count = 0;
  34. $start = [gettimeofday];
  35. foreach (@words) {
  36.     $count++ if /$str/;
  37.     }
  38. say "interpolate: count=$count interval=" . tv_interval($start);
  39.  
  40.  
Advertisement
Add Comment
Please, Sign In to add comment