in reply to Speed of Array Returns
Ideally you would have the choice, and perl as it happens offers this choice.
Most of the time you want a copy (on the way in and on the way out of a subroutine), because it decouples the subroutine: You can just assume that your parameters are unchanged by a subroutine and that you can change the result of a subroutine without having to think about any side effects if you further process that result (moritz gave a simple example of these side effects).
Think about the last 10 subroutines you wrote. If you changed all these subroutines to return pointers to arrays instead of arrays (or vice versa) would you notice any speed difference? Probably not. And even if you did, could you have the same difference by only speeding up one of these 10? Probably. So is it faster to find that one subroutine to speed up or is it faster to think of how to avoid any side effects any time you write a call to a subroutine and find the bugs afterwards if you make a single mistake there
|
|---|