Try adding this bit of code:

my %results= ( nothing => nothing(), _undef => _undef(), empty => empty(), zero => zero(), );

And you'll end up with:

{ "nothing" => "_undef", "" => "empty", "zero" => 0, }

Which shows why you should return undef; for failure in functions that never return more than one item.

but twice as fast

*sigh* removing the obfuscation from your results (have you heard of 'cmpthese' -- much better if you want to post your premature nano-optimization results)

_undef: 11,363,636.36/s empty: 9,174,311.93/s nothing: 20,000,000.00/s zero: 166,666,666.67/s

How can you pass up the 10-times faster version that does return 0?? I mean, all you have to do is have a script that returns 100million times, and it will suddenly run... a couple of seconds faster!!

Of course, stepping back from premature nano-optimization, we see that the fastest that such a script with 100million returns could possibly run before optimizing is:

sub nothing { return (); } nothing() for 0..100_000_000; warn time()-$^T;

92 seconds. So our "10-times faster" has become "2% faster" and that is for a trivial and useless script. If your script actually did something useful, you'll almost certainly have something more like "0.02% faster". In other words, your benchmark is meaningless. Yes, meaningless.

Pick between return (); vs. return; based on something other than speed. Both are of such trivial cost that the difference will never be noticed in any working code that does something useful or interesting.

- tye        

Updated: s/cmpthere/cmpthese/. Thanks, Limbic~Region.


In reply to Re: Functions that return nothing, nada, failure... (depends) by tye
in thread Functions that return nothing, nada, failure... by leriksen

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.