Subroutines:: Return Type Vs Performance 
Author Message
 Subroutines:: Return Type Vs Performance

"Subroutines :: The flexibility of returning multiple values (eg in array
context) imposes a performace penalty." Is this true?

Can someone please explain why the performance is going to be hit if I
return for eg. 10 values instead of 1?

Regards,
Kasp.
--
"Accept that some days you are the pigeon and some days the statue."
"A pat on the back is only a few inches from a kick in the butt." - From
Dilbert.



Fri, 30 Sep 2005 13:55:04 GMT  
 Subroutines:: Return Type Vs Performance

Quote:

> "Subroutines :: The flexibility of returning multiple values (eg in array
> context) imposes a performace penalty."

What is it that you are quoting from?

You should provide an attribution for quotes.

(You must have meant "list context" rather than "array context".)

Quote:
> Is this true?

Yes.

Quote:
> Can someone please explain why the performance is going to be hit if I
> return for eg. 10 values instead of 1?

The question (and answer) are basic Computer Science, there is
nothing Perl-specific about the model commonly used for
subroutine calls.

When a subroutine is called, all of its arguments are pushed onto
the stack by the main code, then popped off of the stack in the
subroutine code.

With 1 argument, there is 1 push and 1 pop. With 10 arguments there
are 10 pushes and 10 pops.

If you return a reference to an array instead of a list, then there
is only 1 push/pop regardless of the size of the array.

--
    Tad McClellan                          SGML consulting

    Fort Worth, Texas



Fri, 30 Sep 2005 15:43:46 GMT  
 Subroutines:: Return Type Vs Performance
Yes Tad, I meant List Context. Sorry about that.

This quote is coming from a program's comment that I have my hands on
now...probably one of the old coders who is now not around :-(

I understand the concept of stack that you mentioned. The fact of a variable
being passed involves it being pushed and later popped from the stack.
But I want to now if there are any idiosyncrasies in Perl. For eg the way it
handles lists etc that do something special when being passed around.
As in C/C++, arrays are not passed-by-value, rather their address is
passed - making it a pass-by-reference. Also when I want to return an array
from a routine, I return it's address back to the calling function.

TIA.
--
"Accept that some days you are the pigeon and some days the statue."
"A pat on the back is only a few inches from a kick in the butt." - Dilbert.



Fri, 30 Sep 2005 16:30:10 GMT  
 Subroutines:: Return Type Vs Performance

Quote:

> Yes Tad, I meant List Context. Sorry about that.

> This quote is coming from a program's comment that I have my hands on
> now...probably one of the old coders who is now not around :-(

Someone who didn't see, or just didn't understand this Perl FAQ:

   What is the difference between a list and an array?

Quote:
> I understand the concept of stack that you mentioned. The fact of a variable
> being passed involves it being pushed and later popped from the stack.
> But I want to now if there are any idiosyncrasies in Perl. For eg the way it
> handles lists etc that do something special when being passed around.

What kind of lists do something special when being passed around?

I can't think of one.

Did you mean "array" instead of "list" again?

Are you talking about something other than a tied array?

Got code?

Quote:
> As in C/C++, arrays are not passed-by-value, rather their address is
> passed -

A "reference" is not an "address", as in C/C++.

In Perl a reference is a reference, you cannot do arithmetic on
it like you can in C et. al.

Quote:
> making it a pass-by-reference.

But you're right in that it _is_ pass-by-reference, which is why


Effectively converting it to pass-by-value.

Unless you _want_ it to be pass-by-reference of course.

Quote:
> Also when I want to return an array
> from a routine, I return it's address back to the calling function.

You return a "reference" rather than an address. This is not C.  :-)

--
    Tad McClellan                          SGML consulting

    Fort Worth, Texas



Fri, 30 Sep 2005 22:29:25 GMT  
 Subroutines:: Return Type Vs Performance

Quote:

> "Subroutines :: The flexibility of returning multiple values (eg in array
> context) imposes a performace penalty." Is this true?

Yes.  However, a substantial amount of this penalty has already been paid
the moment you decided to use Perl.  Perl already has the overhead
necessary to support multiple value returns, whether you actually use them
or not.

BTW, note that the wantarray function should have been named wantlist.

Quote:
> Can someone please explain why the performance is going to be hit if I
> return for eg. 10 values instead of 1?

Because in order to return 10 values, you need to copy 10 things, and
10 is more than 1.  How much does this cost you?  Not much, I'm thinking.

And, if you come up with some clumsy way of returning one thing rather
than ten when returning ten things is the natural thing to do, the
clumsiness is likely to cost you more than you'll save.

Xho

--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service              New Rate! $9.95/Month 50GB



Sun, 02 Oct 2005 23:13:47 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. An attempt to compare the performance of perl vs compiled perl vs C

2. file access vs DB access relative performance

3. performance gain by using subroutine prototypes?

4. Poor performance isqlperl vs perl5+dbi+dbd

5. Performance of DBD::Oracle Vs. JDBC

6. poor performance of activeperl vs MKS Perl

7. Performance of perl vs awk and sed

8. Performance perl4 vs. perl5

9. poor performance of activeperl vs MKS Perl

10. Performance of Perl scripts vs. compile C code

11. Poor performance isqlperl vs perl5+dbi+dbd

12. performance of java vs perl

 

 
Powered by phpBB® Forum Software