Did you forget an explicit return? Forget? No. Omit? Quite likely. Live with it. Cos nothing in this world is going to make me give up a 400% speed gain:
Just in case some (Perl) newbie doesn't get that subroutines return the value of their last expression and need a keyword to tell them so. No way José.[0] Perl> sub a(){ 1 }; sub b(){ return 1; };; [0] Perl> cmpthese -1,{ + a=>q[ a() for 1 .. 1000;], b=>q[b() for 1 .. 1 +000;] };; Rate b a b + 4668/s -- -80% a 23195/s 397% -- [download]
Your benchmark is rather bogus. The performance "hit" from the return statement is negligible. It's the prototype for no arguments with no return statement that gives a seeming big return (I'm guessing the interpreter is optimizing it to nearly a no-op).
$ perl -E 'use Benchmark qw(timethese cmpthese); sub a() { 1 }; sub b( +) { return 1 }; cmpthese -1,{ a=>q[ a() for 1 .. 1000;], b=>q[b() for + 1 .. 1000;] };' Rate b a b 2694/s -- -88% a 23209/s 761% -- $ perl -E 'use Benchmark qw(timethese cmpthese); sub a { 1 }; sub b { +return 1 }; cmpthese -1,{ a=>q[ a() for 1 .. 1000;], b=>q[b() for 1 . +. 1000;] };' Rate b a b 2595/s -- -8% a 2823/s 9% --
Is there a performance gain from not using return in this spot? Yes.
Is it a premature micro-optimization which is unlikely to have anything but a negligible impact the vast majority of real world code? Yes.
Would I consider this performance gain an invalid reason to skip a return statement on any code that hasn't been profiled and clearly shown to benefit from this micro-optimization? Yes.
In reply to Re^6: The Most Essential Perl Development Tools Today
by topher
in thread The Most Essential Perl Development Tools Today
by Tommy
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |