Withigo has asked for the wisdom of the Perl Monks concerning the following question:
Dear Monks,
I have a question about how Perl internally works when returning from subroutines. I'm in a debate about whether it is a meaningful optimization to not use "return" and "return 1" in subroutines. What has been instead been proposed is to do something like this:
Let's say foo() and bar() are called millions of times. The claim is the foo() is creating a new scalar (SV) upon each call and returning it, and that bar() is not making perl internally use more memory/cpu resources than it already has allocated. The claim is that perl does something magical with the result of '1 == 1' and '1 == 0', and does not create a new SV to represent the result of that operation.
However, I would think that something so fundamental as the simple return values from subs would already be optimized internally so as to not add needless overhead.
Can anyone confirm any of this?
Thanks!
I have a question about how Perl internally works when returning from subroutines. I'm in a debate about whether it is a meaningful optimization to not use "return" and "return 1" in subroutines. What has been instead been proposed is to do something like this:
use constant Some::Module::TRUE => 1 == 1; use constant Some::Module::FALSE => 1 == 0; sub foo { return; } sub bar { return Some::Module::FALSE; }
Let's say foo() and bar() are called millions of times. The claim is the foo() is creating a new scalar (SV) upon each call and returning it, and that bar() is not making perl internally use more memory/cpu resources than it already has allocated. The claim is that perl does something magical with the result of '1 == 1' and '1 == 0', and does not create a new SV to represent the result of that operation.
However, I would think that something so fundamental as the simple return values from subs would already be optimized internally so as to not add needless overhead.
Can anyone confirm any of this?
Thanks!
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Not returning scalars(SV) to optimize and save memory?
by ikegami (Patriarch) on Dec 06, 2007 at 20:19 UTC | |
Re: Not returning scalars(SV) to optimize and save memory?
by perrin (Chancellor) on Dec 06, 2007 at 23:48 UTC | |
Re: Not returning scalars(SV) to optimize and save memory?
by gamache (Friar) on Dec 06, 2007 at 20:16 UTC | |
by tye (Sage) on Dec 06, 2007 at 20:25 UTC | |
by gamache (Friar) on Dec 06, 2007 at 20:30 UTC | |
by ikegami (Patriarch) on Dec 06, 2007 at 20:30 UTC | |
Re: Not returning scalars(SV) to optimize and save memory?
by RaduH (Scribe) on Dec 06, 2007 at 20:19 UTC |
Back to
Seekers of Perl Wisdom