#!/usr/bin/perl -w use strict; use Benchmark qw(cmpthese); my $pattern=join "|",qw(a ab abc abcde ef gh ghij qrst nqmz stuv); my $qr=qr/$pattern/; my @candidates=qw(abc zzz stuv asasdfasdfaf qqqqqqqqqqqqqqqq lkasjh adsd qqqqqqqqqqqqabc); sub preComp { my $result=scalar grep /$pattern/o,@candidates; return $result; } sub preCompQR { my $result=scalar grep /$qr/o,@candidates; return $result; } sub noPrecomp { my $result=scalar grep /$pattern/,@candidates; return $result; } print "Working from pattern '$pattern', qr='$qr'\n"; print "preComp finds ",preComp(),"\n"; print "preCompQR finds ",preCompQR(),"\n"; print "noPrecomp finds ",noPrecomp(),"\n"; cmpthese(-3, { preComp => \&preComp, preCompQR => \&preCompQR, noPrecomp => \&noPrecomp } );