in reply to Comparing Regular Expressions

There are some good benchmarking modules, but there may be easier ways:
perl -le '$text = <STDIN>; $T=time; $TestText=$text; for (1..$ARGV[0]) + { $text =~ s/\s+,\s+|\s+,|,\s+/,/g; } print time - $T; $T=time; $Tes +tText=$text; for (1..$ARGV[0]) { $text =~ s/\s*,\s*/,/g; } print time + - $T;' 1000000 </tmp/text.file
This oneliner just runs your two regexps on the same text for a specific number of loop runs. Use a number which takes at least 10, better more, seconds for each loop.
This could also be improved by using Time::HiRes.

Replies are listed 'Best First'.
Re^2: Comparing Regular Expressions
by JavaFan (Canon) on Sep 01, 2009 at 00:31 UTC
    Your benchmark is bogus.

    Realize that s/// actually modifies the string it's operating on. After the first iteration, there will be no spaces around commas left in $text.

    Furthermore, you assign to $TestText, but you never actually use its value.

      You're completly right. Thank you for the hints.

      perl -le '$text = <STDIN>; $T=time; for (1..$ARGV[0]) { my $TestText = + $text; $TestText =~ s/\s+,\s+|\s+,|,\s+/,/g; } print time - $T; $T=t +ime; $TestText=$text; for (1..$ARGV[0]) { my $TestText = $text; $Test +Text =~ s/\s*,\s*/,/g; } print time - $T;' 1000000 </tmp/text.file
      This should correct all bugs.