Here's another question borne out of idle curiosity: what is the most efficient way to force a list context? I used to think that it'd be assigning to the empty list, but that's not the case, at least not always, as the results below show.

First, here's the set-up:

use Benchmark 'cmpthese'; sub gr { # Guttman-Rosler transform map { substr( $_, 3 ) } sort map { sprintf( '%03d', /abc(\d+)xyz/ ) . $_ } @_; } srand 0; my @u = map 'abc' . int( rand 1000 ) . 'xyz', 1..500; cmpthese( -1, { s_aa => sub { my @s = sort @u }, s_fi => sub { ( sort @u )[ 0 ] }, s_na => sub { () = sort @u } } ); cmpthese( -1, { g_aa => sub { my @s = gr @u }, g_fi => sub { ( gr @u )[ 0 ] }, g_na => sub { () = gr @u } } );
Two sets of benchmarks, corresponding to various ways of forcing list contexts on calls to two different functions: Perl's built-in lexicographic sort function (these are the s_* items), and gr, a simple implementation of a Guttman-Rosler sort (this is basically the subroutine guttros posted by friedo here; these are the g_* items). (The results will show that either set of benchmarks would have been sufficient illustration, but it's nice to have the extra datapoint by way of crosscheck.)

And here are the results:

Rate s_aa s_na s_fi s_aa 1037/s -- -49% -49% s_na 2017/s 95% -- -1% s_fi 2036/s 96% 1% -- Rate g_na g_aa g_fi g_na 237/s -- 0% -14% g_aa 237/s 0% -- -14% g_fi 275/s 16% 16% --
There you have it: fetching the first element unequivocally beats assigning to (). I find this very surprising. After all, fetching the first element requires some honest work, but assigning to a null list is something so nominal that I would have expected it to be pretty much the list counterpart of scalar.

The approaches above are what I could think of as likely candidates for the lowest-overhead list-forcing method. But the results tell me that my intuition on these matters is a poor guide. Are there more efficient methods?

More to the point, why isn't there a direct way to just tell perl we want a list context, just like we can do with scalar context? It's silly to have trick perl into it.

the lowliest monk


In reply to What's most efficient way to get list context? by tlm

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.