One for largish and one for small lists:
$ perl -MBenchmark -e'timethese(10000, { map => sub { @_ = map 1, 1..1 +000 }, x => sub { @_ = (1)x1000 }})' Benchmark: timing 10000 iterations of map, x... map: 29 wallclock secs (27.09 usr + 0.24 sys = 27.33 CPU) @ 36 +5.90/s (n=10000) x: 14 wallclock secs (13.36 usr + 0.09 sys = 13.45 CPU) @ 74 +3.49/s (n=10000) $ perl -MBenchmark -e'timethese(1000000, { map => sub { @_ = map 1, 1. +.5 }, x => sub { @_ = (1)x5 }})' Benchmark: timing 1000000 iterations of map, x... map: 17 wallclock secs (15.55 usr + 0.15 sys = 15.70 CPU) @ 63 +694.27/s (n=1000000) x: 9 wallclock secs ( 8.44 usr + 0.11 sys = 8.55 CPU) @ 11 +6959.06/s (n=1000000)
The message is clear - a pretty precise 100% speed benefit from x. Of note is what happens internally:
$ perl -MO=Deparse,-x7 -e'timethese(1000000, { map => sub { @_ = map 1 +, 1..5 }, x => sub { @_ = (1)x5 }})' timethese 1000000, {'map', sub { @_ = map(1, (1, 2, 3, 4, 5)); } , 'x', sub { @_ = (1) x 5; } }; -e syntax OK
I'll spare you the corresponding output from 1..1000 - a list of 1000 numeric literals. In other words not only does the map take twice as long in every instance, it also consumes more memory - grossly so, for large lists.

Makeshifts last the longest.


In reply to Re^3: Generating an array of n identical elements by Aristotle
in thread Generating an array of n identical elements by bronto

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.