I'm not really sure what is happening and unfortunatly I didn't seem
to keep my benchmarking code. I wrote about it at
http://peter.makholm.net/2009/05/26/benchmarking-is-hard/
I might have used Jason Switchers code modified to only use one
needle and array.
//Makholm
I don't see how grep is faster than anything.
-sln
---------------------------
use strict;
use warnings;
use Benchmark ':hireswallclock';
my @rxs = (qr/one/, qr/two/, qr/three/);
my @strings =
# (
# 'two three one',
# 'one three ',
# 'three two',
# 'three',
# ' '
# );
(
'one two three',
'one two three',
'one two three',
'one two three',
);
my ($pass,$val,$string,$rxs,$t0,$t1,$tdif);
$t0 = new Benchmark;
for (1..10_000) {
foreach $string (@strings) {
if ( not $val = grep $string !~ $_, @rxs)
{
# print "grep !$val = yes\n";
} else {
# print "grep !$val = no\n";
}
}
}
$t1 = new Benchmark;
print "grep code took:",timestr( timediff($t1, $t0) ),"\n";
$t0 = new Benchmark;
for (1..10_000) {
STRING:
for (@strings) {
for $rxs (@rxs) {
if ( !/$rxs/)
{
# print "var && no\n";
next STRING;
}
}
# print "var && yes\n";
}
}
$t1 = new Benchmark;
print "(1)&& code took:",timestr( timediff($t1, $t0) ),"\n";
$t0 = new Benchmark;
for (1..10_000) {
for (@strings) {
if ( /one/ && /two/ && /three/)
{
# print "const && yes\n";
} else {
# print "const && no\n";
}
}
}
$t1 = new Benchmark;
print "(2)&& code took:",timestr( timediff($t1, $t0) ),"\n";
__END__
Output:
grep code took:0.207262 wallclock secs ( 0.20 usr + 0.00 sys = 0.20 CPU)
(1)&& code took:0.246479 wallclock secs ( 0.25 usr + 0.00 sys = 0.25 CPU)
(2)&& code took:0.0333102 wallclock secs ( 0.03 usr + 0.00 sys = 0.03 CPU)