So in short, returning an array ref is easier to code and can be significantly more efficient if you're returning a lot of values

Been playing around with some stuff, trying to verify the above quote.

I came up with this (perhaps it's sophistic):
use warnings; use Benchmark; use Inline C => Config => BUILD_NOISY => 1; use Inline C => <<'EOC'; void foo(AV * x) { dXSARGS; SV ** elem; int len, i; len = av_len(x) + 1; sp = mark; for(i = 0; i < len; i++) { elem = av_fetch(x, i, 0); XPUSHs(*elem); } PUTBACK; XSRETURN(len); } AV * bar(AV * x) { dXSARGS; SV ** elem; int len, i; sp = mark; len = av_len(x) + 1; for(i = 0; i < len; i++) { elem = av_fetch(x, i, 0); XPUSHs(*elem); } return x; } EOC $len = 2000000; @x = (); for(1 .. $len) {push @x, $_} timethese (1, { 'ref' => '$ret = bar(\@x)', 'list' => '@ret = foo(\@x)', });
For me that produces:
Benchmark: timing 1 iterations of list, ref... list: 1 wallclock secs ( 0.36 usr + 0.11 sys = 0.47 CPU) @ 2 +.13/s (n=1 ) (warning: too few iterations for a reliable count) ref: 0 wallclock secs ( 0.11 usr + 0.00 sys = 0.11 CPU) @ 9 +.17/s (n=1 ) (warning: too few iterations for a reliable count)
The difference is certainly *measurable*, but I wonder if it's all that *significant* (given the size of the array). I also wonder a little about the assertion that it's easier to return an AV* than a list. For most of the stuff that I have done, I've found it easier to return a list - even though the coding may involve additional keystrokes. (Admittedly, most of the stuff I've done is Inline based ... and I'm a simpleton :-)

Cheers,
Rob

In reply to Re^2: Return array or list from an XSUB? by syphilis
in thread Return array or list from an XSUB? by smee30

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.