Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Returning lists vs arrays

by BrowserUk (Patriarch)
on Jan 20, 2003 at 17:01 UTC ( #228395=note: print w/replies, xml ) Need Help??


in reply to Returning lists vs arrays

My thought on this is that, if the dictates of your application lend or require you to return a list, rather than a ref, try and return the list without building an array first, if it's sensible to do so.

As a example, rather than

sub get_3d_xyz { $self = shift; my @xyz = ( $self->get_x() , $self->get_y() , $self->get_z() ); return @xyz; }

save allocating and building the intermediate lexical array and do

sub get_3d_xyz { $self = shift; return ( $self->get_x(), $self->get_y(), $self->get_z() ); }

Or even,

use constant SELF => 0; sub get_3d_xyz { ( $_[SELF]->get_x() , $_[SELF]->get_y() , $_[SELF]->get_z() ); }

Each of those steps shaves a few micro/milliseconds of the operation. Not a lot as and of itself, but at the heart of a 3d-graphic class, significant enough.

Updated: Corrected a couple of typos. Thanks ihb.


Examine what is said, not who speaks.

The 7th Rule of perl club is -- pearl clubs are easily damaged. Use a diamond club instead.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://228395]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2022-09-29 15:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I prefer my indexes to start at:




    Results (125 votes). Check out past polls.

    Notices?