Dave said:
Mark Seger wrote:
Not by a long shot on my machine, running Perl 5.8.7.
=code
use Benchmark qw

hireswallclock cmpthese);
my $pat = 'A Perl Paaattern';
my $match = 0;
cmpthese(0, {
'Code1' => '$match = 1 if $pat =~/aaa/ || $pat=~/bbb/ || $pat=~/ccc/',
'Code2' => '$match = 1 if $pat =~/aaa|bbb|ccc/',
});
I've never used benchmark, but it sounds neat. But how could it get
such a different number from my test? Here's what I did:
test1.pl
#!/usr/bin/perl -w
$a="abcdefghijklkmbnopqrstuvwxyz";
for ($i=0; $i<1000000; $i++)
{
my $z=1 if $a=~/aaa|bbb|ccc/;
}
test2.pl
#!/usr/bin/perl -w
$a="abcdefghijklkmbnopqrstuvwxyz";
for ($i=0; $i<1000000; $i++)
{
my $z=1 if $a=~/aaa/ || $a=~/bbb/ || $a=~/ccc/;
}
[root@cag-dl380-01 test]# time ./test1.pl
real 0m7.492s
user 0m7.420s
sys 0m0.000s
[root@cag-dl380-01 test]# time ./test2.pl
real 0m0.956s
user 0m0.950s
sys 0m0.000s
Rate Code1 Code2
___________________________
Code1 2775928/s -- -62%
Code2 7368521/s 165% --
___________________________
Code1 2888905/s -- -63%
Code2 7777375/s 169% --
___________________________
Code2 2647141/s -- -64%
Code1 7289831/s 175% --
___________________________
Code1 2742926/s -- -62%
Code2 7313196/s 167% --
___________________________
Code1 2803761/s -- -61%
Code2 7211258/s 157% --
---------------------------
I also tried converting things to use Benchmark and got similar numbers
as above but am not sure how to interpret this as there was nothing in
the manpage I read, but I'll continute to look. Is this saying that the
first code fragment executed about 3M times/sec and the second 7M? I'm
not sure what the percentages represent, but if they're relative to
something, it feels like 'code1' is almost 3 times faster than 'code2'
which seems to correlate with the numbers/second. If so then there IS a
difference though not as much as I'm seeing.
I also tried running my code with Benchmark but am getting some warnings
I'm not sure about. If I try "my $a" I get a zillion uninit values that
make no sense, not to mention the subroutine redefinition message.
Maybe I need a different version of Benchmark? Anyhows, here's my code:
#!/usr/bin/perl -w
use Benchmark qw

hireswallclock cmpthese);
$a="abcdefghijklkmbnopqrstuvwxyz";
cmpthese(0,
{
'Code1' => '$z=1 if $a=~/aaa|bbb|ccc/',
'Code2' => '$z=1 if $a=~/aaa/ || $a=~/bbb/ || $a=~/ccc/'
});
and the results:
[root@cag-dl380-01 test]# ./bench1.pl
Subroutine Benchmark::mytime redefined at
/usr/lib/perl5/5.8.0/Benchmark.pm line 450.
Name "main::a" used only once: possible typo at ./bench1.pl line 5.
Rate Code1 Code2
Code1 144273/s -- -90%
Code2 1494766/s 936% --
Is the implication that I ran 10 times more iterations using the second
form?
I also tried using the 'timethese' function in Benchmark, simply
replacing the references to 'cmpthese' so I won't bore you with the
code. Here's what that did and as you can see I'm still getting
warnings and errors so I'm not sure how trustworthy the results are
[root@cag-dl380-01 test]# ./bench2.pl
Subroutine Benchmark::mytime redefined at
/usr/lib/perl5/5.8.0/Benchmark.pm line 450.
Name "main::a" used only once: possible typo at ./bench2.pl line 5.
Benchmark: running Code1, Code2 for at least 3 CPU seconds...
Code1: 3.28681 wallclock secs ( 3.26 usr + 0.00 sys = 3.26 CPU)
@ 146238.34/s (n=476737)
Code2: 3.20847 wallclock secs ( 3.21 usr + 0.00 sys = 3.21 CPU)
@ 1401934.58/s (n=4500210)
-mark