I've been working on an infrastructure module that needs to be as fast as possible. As I don't have the time to trudge the XS learning curve, I've been working on speeding the Perl up.

One of the things I found when examining usage is that there's an awful lot of the following:

sub foo { ..... return @foo; } my $num_foo = foo( @args );

So, I was thinking - would using wantarray be faster than not? Or, alternately, does Perl implicitly use wantarray for you? Out comes Benchmark!

Using 5.8.0 on Solaris9 ...

my @x = 1 .. 1_000_000; sub want { return wantarray ? @x : $#x + 1; } sub nowant { return @x; } cmpthese( -10, { '1nowant' => sub { my $x = nowant(); }, '2want' => sub { my $x = want(); }, '3nowant_list' => sub { my $x = () = nowant(); }, '4want_list' => sub { my $x = () = want(); }, }); -------- Benchmark: running 1nowant, 2want, 3nowant_list, 4want_list for at lea +st 10 CPU seconds... 1nowant: 10 wallclock secs (10.25 usr + 0.00 sys = 10.25 CPU) @ 58 +2570.73/s (n=5971350) 2want: 10 wallclock secs (10.05 usr + 0.00 sys = 10.05 CPU) @ 57 +8237.81/s (n=5811290) 3nowant_list: 11 wallclock secs (10.84 usr + 0.00 sys = 10.84 CPU) @ + 1.11/s (n=12) 4want_list: 11 wallclock secs (10.66 usr + 0.00 sys = 10.66 CPU) @ 1 +.13/s (n=12) Rate 3nowant_list 4want_list 2want 1now +ant 3nowant_list 1.11/s -- -2% -100% -1 +00% 4want_list 1.13/s 2% -- -100% -1 +00% 2want 578238/s 52234049% 51366692% -- +-1% 1nowant 582571/s 52625456% 51751600% 1% + --

The results seem to say that Perl puts a wantarray in for you, if it seems appropriate. What looks to be proof to me is that if you force list context, but still assign to a scalar, you lose a ton of speed. In fact, wantarray seems to be slightly slower. (It actually isn't, if you reverse the order of running. Plus, my personal rule of thumb is that anything which doesn't provide 5% or more of difference is a wash.)

So, it seems that this micro-optimization actually doesn't help.

Update: Fixed the code and re-ran it. The values changed a bit.

Benchmark: running 1nowant, 2want, 3nowant_list, 4want_list for at lea +st 10 CPU seconds... 1nowant: 13 wallclock secs (10.67 usr + 0.00 sys = 10.67 CPU) @ 55 +8715.46/s (n=5961494) 2want: 12 wallclock secs (10.66 usr + 0.00 sys = 10.66 CPU) @ 39 +0887.71/s (n=4166863) 3nowant_list: 11 wallclock secs (10.75 usr + 0.00 sys = 10.75 CPU) @ + 1.12/s (n=12) 4want_list: 11 wallclock secs (10.72 usr + 0.00 sys = 10.72 CPU) @ 1 +.12/s (n=12) Rate 3nowant_list 4want_list 2want 1now +ant 3nowant_list 1.12/s -- -0% -100% -1 +00% 4want_list 1.12/s 0% -- -100% -1 +00% 2want 390888/s 35016924% 34919202% -- - +30% 1nowant 558715/s 50051494% 49911815% 43% + --

Using wantarray actually seems to be a penalty, not an improvement.

------
We are the carpenters and bricklayers of the Information Age.

Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose

I shouldn't have to say this, but any code, unless otherwise stated, is untested


In reply to Optimizing with wantarray by dragonchild

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.