But what does matter is how the arrays were made:
my $str = shift || "17:43:33:21:23:19:27:6"; my @a1 = (); my @a2 = (); my @a = split /:/, $str; while ( my ( $p, $ad ) = splice @a, 0, 2 ) { push @a1, map {$ad} (1..$p); push @a2, ($ad) x $p; } use Benchmark 'cmpthese'; print "Accessing element 0:\n"; cmpthese(-1, { made_by_map => sub { $a1[0]; }, made_by_x => sub { $a2[0]; } }); print "\nAccessing element 50:\n"; cmpthese(-1, { made_by_map => sub { $a1[50]; }, made_by_x => sub { $a2[50]; } }); print "\nAccessing element 99:\n"; cmpthese(-1, { made_by_map => sub { $a1[99]; }, made_by_x => sub { $a2[99]; } }); print "\nAccessing random element:\n"; cmpthese(-1, { made_by_map => sub { $a1[rand 100]; }, made_by_x => sub { $a2[rand 100]; } });
Using the same string as in the problem (i.e. with no argument passed), we get:
Accessing element 0: Rate made_by_x made_by_map made_by_x 7554581/s -- -11% made_by_map 8479758/s 12% -- Accessing element 50: Rate made_by_x made_by_map made_by_x 7788171/s -- -4% made_by_map 8095273/s 4% -- Accessing element 99: Rate made_by_map made_by_x made_by_map 8058884/s -- -9% made_by_x 8882975/s 10% -- Accessing random element: Rate made_by_map made_by_x made_by_map 1724352/s -- -2% made_by_x 1758653/s 2% --
In other words, the array made by map is faster accessing elements close the front, but slower accessing elements near the end (enough so that the random element access test goes in favor of the list created by x)

However, the results for different initial strings aren't really making any sense - when I use a string of one hundred repetitions of "1:8", I get essentially opposite results: the x array is significantly faster at doing [0], (by ~ 20%) there's a tie for [50] and the map array wins big at doing [99] (by ~ 10%), and then map wins at the random index test.

So far so good, but when I use "100:8", I get the map array winning at [50] and [99] and only losing by 1% on [0], yet still losing the random element test by 2%.

Are all my results just noise? I think I should make this a new SoPW post.

-- @/=map{[/./g]}qw/.h_nJ Xapou cets krht ele_ r_ra/; map{y/X_/\n /;print}map{pop@$_}@/for@/

In reply to Re^8: $str to %hash to @ary by fizbin
in thread $str to %hash to @ary by abaxaba

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.