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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |