Advertisement
Kenosis

Regex vs. Split

Feb 6th, 2013
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 1.09 KB | None | 0 0
  1. use strict;
  2. use warnings;
  3. use Benchmark qw(cmpthese);
  4.  
  5. chomp( my @lines = <DATA> );
  6.  
  7. sub regExLines {
  8.     my ( $yes, $no );
  9.  
  10.     for my $string (@lines) {
  11.         next if $string !~ /^\*.+/ or $string =~ /Created/;
  12.         if ( $string =~
  13.             /^(?>\*\s+(\w\w\w-\d\d-\d\d\d\d)\s+(\w+)\s+(\d+)\s+)(?!Created)/ )
  14.         {
  15.             $yes++;
  16.         }
  17.         else {
  18.             $no++;
  19.         }
  20.     }
  21. }
  22.  
  23. sub splitLines {
  24.     my ( $yes, $no );
  25.  
  26.     for my $string (@lines) {
  27.         next if $string !~ /^\*.+/ or $string =~ /Created/;
  28.         my ( undef, $date, $user, $change, $desc ) = split ' ', $string, 5;
  29.  
  30.         if ( $desc !~ /^Created/ ) {
  31.             $yes++;
  32.         }
  33.         else {
  34.             $no++;
  35.         }
  36.     }
  37. }
  38.  
  39. cmpthese(
  40.     -5,
  41.     {
  42.         regExLines => sub { regExLines() },
  43.         splitLines => sub { splitLines() },
  44.     }
  45. );
  46.  
  47. #               Rate regExLines splitLines
  48. #splitLines 356059/s         --        -6%
  49. #regExLines 377659/s         6%         --
  50.  
  51. __DATA__
  52. * JAN-01-2001   bugsbunny     1234     Created Module
  53.   JAN-01-2001   bugsbunny     1234     Created Module
  54. * DEC-12-2012   bugsbunny     5678     Modified Module
  55.   DEC-12-2012   bugsbunny     5678     Modified Module
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement