This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

Regex vs. Split

By: Kenosis on Feb 6th, 2013  |  syntax: Perl  |  size: 1.09 KB  |  views: 8  |  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. 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
clone this paste RAW Paste Data