in reply to Re^2: Surprisingly poor regex performance
in thread Surprisingly poor regex performance
(original takes about 25 seconds per iteration on my machine)#!/usr/local/bin/perl -w use strict; use Benchmark; my $pat = 'foo'; $/=undef; open F,"<","/usr/share/dict/words"; my $mmap = <F>; close F; timethese( 100, { # 'orig' => sub { # while ($mmap =~ m/^([^\n]*$pat[^\n]\n)/omg) { # warn $1; # } # }, 'fast' => sub { while ($mmap =~ m/\n?(.*$pat.*\n)/omg) { warn $1; } }, 'better' => sub { while ($mmap =~ m/(?:\A|\n)(.*$pat.*\n)/omg) { warn $1; } }, } ) __END__ Benchmark: timing 100 iterations of better, fast... better: 26 wallclock secs (23.13 usr + 0.06 sys = 23.19 CPU) @ 4 +.31/s (n=100) fast: 41 wallclock secs (39.73 usr + 0.09 sys = 39.82 CPU) @ 2 +.51/s (n=100)
|
|---|