Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: Kenosis on Nov 20th, 2012  |  syntax: Perl  |  size: 1.19 KB  |  views: 9  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. use strict;
  2. use warnings;
  3. use Benchmark qw/cmpthese/;
  4.  
  5. my @list1 = qw/a.f c data g j/;
  6. my @list2 = qw/myfile.txt a.file.txt data.txt otherfile.txt jargon.txt/;
  7.  
  8. my $regex = join '|', map "\Q$_\E", @list1;
  9. my $regexQR = qr/^(?:$regex)/;
  10.  
  11. sub theGrepQR {
  12.         my @list3 = grep $_ !~ $regexQR, @list2;
  13. }
  14.  
  15. sub theGrepNoQR {
  16.         my @list3 = grep !/^(?:$regex)/, @list2;
  17. }
  18.  
  19. sub theGrepOModifier {
  20.         my @list3 = grep !/^(?:$regex)/o, @list2;
  21. }
  22.  
  23. sub thePush {
  24.         my @list3;
  25.         for my $match (@list1) {
  26.                 push @list3, grep { substr( $_, 0, length($match) ) eq $match } @list2;
  27.         }
  28. }
  29.  
  30. cmpthese(
  31.         -5,
  32.         {
  33.                 theGrepQR        => sub { theGrepQR() },
  34.                 theGrepNoQR      => sub { theGrepNoQR() },
  35.                 theGrepOModifier => sub { theGrepOModifier() },
  36.                 thePush          => sub { thePush() },
  37.         }
  38. );
  39.  
  40. __DATA__
  41.                      Rate      thePush    theGrepQR theGrepNoQR theGrepOModifier
  42. thePush          174874/s           --         -41%        -56%             -67%
  43. theGrepQR        296601/s          70%           --        -26%             -44%
  44. theGrepNoQR      398394/s         128%          34%          --             -25%
  45. theGrepOModifier 533505/s         205%          80%         34%               --
clone this paste RAW Paste Data