in reply to Functions that return nothing, nada, failure...

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.

Replies are listed 'Best First'.
Re: Re: Functions that return nothing, nada, failure... (depends)
by leriksen (Curate) on Jun 02, 2004 at 01:28 UTC
    I agree with everything your say ;-) but I have a different slant on many of your points.

    Which shows why you should return undef;
    good point, yes that is a case when it would be use.

    have you heard of 'cmpthese' -- much better if you want to post your premature nano-optimization results
    yes I have, I just like the otherway more. Maybe one day I'll prefer the cmpthese() style.

    I also know that the benchmarks are, in pure real world terms, complete bullshit - but I wanted to know what the speed difference between return; and return() was, with no other considerations. That was the only reason for benchmarking them.

    The point was , if they bith give the same result in any context, when would you chose one over the other, though I agree it was misleading to say one was twice as fast - it could indicate I thought that was reason enough.

    How can you pass up the 10-times faster version that does return 0
    Easy, because in list context, it does not indicate failure - therefore in most cases, it is not what you want.

    Pick between return (); vs. return; based on something other than speed.
    I think that was my point - chose return() if it make your code clearer, otherwise always use return;

    +++++++++++++++++
    #!/usr/bin/perl
    use warnings;use strict;use brain;