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...

In reply to Re: Re: Re: Believably slow.. by demerphq
in thread Unbelievably slow.. by kiat

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.