in reply to Regular expression speed issues

For the record:

I think Abigail-II is right:

Benchmark: timing 1000000 iterations of method1, method2, method3... method1: 71 wallclock secs (69.22 usr + 0.13 sys = 69.35 CPU) @ 14 +419.61/s (n=1000000) method2: 39 wallclock secs (39.21 usr + 0.00 sys = 39.21 CPU) @ 25 +503.70/s (n=1000000) method3: 100 wallclock secs (96.76 usr + 0.13 sys = 96.89 CPU) @ 1 +0320.98/s (n=1000000)
I'm mesuring the execution of a for-loop, that gives inacurate results of the absolute time each method takes, but difference in times is well explainable:

The Benchmark-code:

#!/usr/bin/perl use strict; use warnings; use Benchmark; my @test = ("a a a a","a a a", "a a", "a", "a a a a","a a a", "a a", " +a","a a a a","a a a", "a a", "a"); sub method1 { my $text=shift; my $gene=shift; my $count = () = $text =~ /$gene/g; return $count; } sub method2 { my $text=shift; my $gene=shift; my $count=0; my $p=0; ++$count while $p = 1+index( $text, $gene, $p ); return $count; } sub method3 { my $text=shift; my $gene=shift; my $count=0; my $patn = qr/\b$gene\b/; $count++ while $text =~ /$patn/g; return $count } timethese(1000000, { method1 => sub { foreach (@test) {method1($_,'a')}}, method2 => sub { foreach (@test) {method2($_,'a')}}, method3 => sub { foreach (@test) {method3($_,'a')}}, } );

regards,
tomte

edit:: added interpretation of results


Hlade's Law:

If you have a difficult task, give it to a lazy person --
they will find an easier way to do it.