The shifting of elements from the array makes it empty after first iteration.

Ugh, I seem to be extra stupid today. But hey, it's not that easy still:

my @s = '0001' .. '1000'; cmpthese -1,{ d => q[ my @d = @s; ], c => q[ my @c = @s; $c[ $_ ] += 0 for 0 .. $#c; ], a => q[ my @a = @s; $_ += 0 for @a; ], b => q[ my @b = @s; my @new; push @new, $_ + 0 while defined( $_ = + shift @b ) ], };
Yields:
Rate c a b d c 793688/s -- -59% -59% -84% a 1941807/s 145% -- -0% -60% b 1942492/s 145% 0% -- -60% d 4812084/s 506% 148% 148% --
So what do we have, shifting and for (@list) are equally fast? Not so:
my @s = '0000001' .. '1000000'; cmpthese -1,{ d => q[ my @d = @s; ], c => q[ my @c = @s; $c[ $_ ] += 0 for 0 .. $#c; ], a => q[ my @a = @s; $_ += 0 for @a; ], b => q[ my @b = @s; my @new; push @new, $_ + 0 while defined( $_ = + shift @b ) ], };
Gives these results:
Rate c b a d c 764586/s -- -58% -62% -85% b 1803742/s 136% -- -10% -65% a 2007409/s 163% 11% -- -61% d 5119310/s 570% 184% 155% --
Which is logical, at last. Scandal of the century is averted, I stand corrected - the only thing I have to find out is why my tests on actual data consistently show shifting to be faster than for (@list). Not several orders of magnitude faster as it was in that faulty benchmark but considerably so. I believe I have to look for an error...

Regards,
Alex.


In reply to Re^5: Unpacking and converting by dwalin
in thread Unpacking and converting by dwalin

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.