in reply to Not returning scalars(SV) to optimize and save memory?

use Benchmark can tell us what is fastest:
use strict; use constant FALSE => 1==0; use Benchmark qw(:all); cmpthese(1E7, { 'return;' => sub { return }, 'return FALSE;' => sub { return FALSE }, 'return 1==0;' => sub { return 1==0 }, 'implicit' => sub { }, 'implicit FALSE'=> sub { FALSE }, 'implicit 1==0' => sub { 1==0 }, });
Rate return FALSE; implicit FALSE return 1==0; i +mplicit return; implicit 1==0 return FALSE; 18281536/s -- -31% -54% + -63% -80% -92% implicit FALSE 26666667/s 46% -- -33% + -46% -71% -88% return 1==0; 40000000/s 119% 50% -- + -19% -56% -82% implicit 49261084/s 169% 85% 23% + -- -46% -77% return; 91743119/s 402% 244% 129% + 86% -- -58% implicit 1==0 217391304/s 1089% 715% 443% + 341% 137% --
It seems that sub { 1==0 } crushes all comers, 137% faster than its closest competitor, return; aka sub foo from your example. (My guess is that Perl has optimized the subroutine away.) sub bar aka return FALSE comes in dead last, but keep in mind that use constant has rolled FALSE into a closure, so you get overhead for subroutine calls when you use it (which is indicated by both uses of FALSE being in last place).

Not an examination of Perl's internal handling of SVs in subroutine returns, but a useful exploration of relative speed.

Replies are listed 'Best First'.
Re^2: Not returning scalars(SV) to optimize and save memory? ("real world")
by tye (Sage) on Dec 06, 2007 at 20:25 UTC

    And if you come up with a Perl subroutine that you can call 20 million times each second, then you might notice the speed difference. Of course, that will leave almost no time for your subroutine to do anything nor for the caller of the subroutine to do anything.

    Which is why such nano-optimization is a great way to waste your time.

    - tye        

      You caught me mid-brainfart. I wanted to type "relative" but somehow "real world" made it to my fingers first, though I was thinking of it in the context of this being an awful test of real-world conditions.
Re^2: Not returning scalars(SV) to optimize and save memory?
by ikegami (Patriarch) on Dec 06, 2007 at 20:30 UTC
    I disagree with your conclusion. FALSE and 1==0 produce exactly the same code, yet in one place the results say one is 119% times the speed of the other, and in another place the results say one is 715% times the speed of the other.

    A better conclusion would be that the benchmark is useless. The reason might be that external factors (such as cache misses, context swapping) have a stronger influences on that the speed of those operations than the choice of operation.