This benchmark would be far better if you had named the subroutines correctly.
:-) Youll notice that you have the names for the two subs reversed.
Also, I personally wouldn't do a benchmark with an empty block. Put something in it just in case Perl gets smart enough to optimize both of those subroutines down to sub{} and then maybe even just tosses any calls to them becuase they dont really do anything....
use Benchmark 'cmpthese';
#16777216
my @num=0..1000;
my ($x,$y,$z);
my $subhash={
'rangefor' => sub {
for my $num (0 .. 1000) { $x++ }
return();
},
'c-style' => sub {
for( my $num = 0; $num < 1000; $num++) { $y++ }
return();
},
'foreach' => sub {
foreach my $num (@num) { $z++ }
return();
},
};
cmpthese(-3,$subhash) for 1..3;
__END__
Benchmark: running c-style, foreach, rangefor, each for at least 3 CPU
+ secs...
c-style: 3 wall secs ( 3.25 usr + 0 sys = 3.25 CPU) @3039.64/s (n
+=9891)
foreach: 3 wall secs ( 3.30 usr + 0 sys = 3.30 CPU) @3930.39/s (n
+=12986)
rangefor: 3 wall secs ( 3.22 usr + 0 sys = 3.22 CPU) @4133.68/s (n
+=13327)
Rate c-style foreach rangefor
c-style 3040/s -- -23% -26%
foreach 3930/s 29% -- -5%
rangefor 4134/s 36% 5% --
Benchmark: running c-style, foreach, rangefor, each for at least 3 CPU
+ secs...
c-style: 3 wall secs ( 3.16 usr + 0 sys = 3.16 CPU) @3046.59/s (n
+=9612)
foreach: 3 wall secs ( 3.20 usr + 0 sys = 3.20 CPU) @3921.37/s (n
+=12568)
rangefor: 4 wall secs ( 3.18 usr + 0 sys = 3.18 CPU) @4080.31/s (n
+=12955)
Rate c-style foreach rangefor
c-style 3047/s -- -22% -25%
foreach 3921/s 29% -- -4%
rangefor 4080/s 34% 4% --
Benchmark: running c-style, foreach, rangefor, each for at least 3 CPU
+ secs...
c-style: 3 wall secs ( 3.14 usr + 0 sys = 3.14 CPU) @3037.84/s (n
+=9554)
foreach: 3 wall secs ( 3.30 usr + 0 sys = 3.30 CPU) @3941.12/s (n
+=12986)
rangefor: 3 wall secs ( 3.10 usr + 0 sys = 3.10 CPU) @4079.25/s (n
+=12662)
Rate c-style foreach rangefor
c-style 3038/s -- -23% -26%
foreach 3941/s 30% -- -3%
rangefor 4079/s 34% 4% --
---
demerphq
<Elian> And I do take a kind of perverse pleasure in having an OO assembly language...
|