Well, I doubt its brilliant but I believe I can venture a hypothesis:

The scalar version allows perl more opportunities to optimize. For instance it will know at compile time how many elements to copy from the right hand side and it will not have to dynamically allocate space for the target array at run time. Also it will not have to do basic administrative details like emptying the target array before refilling it, in the case of the scalars it need only copy the value or undef the var.

There are probably other less obvious optimizations that Perl can do at compile time with the scalars but must do at run time with the array.

BTW: Next time you might consider writing your killer scalar sub a bit differently:

my $sub=eval 'sub {('.join(',',map{'$s'.$_}0..1000).')=@base}';
:-)

Yves / DeMerphq
---
Writing a good benchmark isnt as easy as it might look.


In reply to Re: list of scalars vs arrays, speed? by demerphq
in thread list of scalars vs arrays, speed? by BUU

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.