in reply to Re: Re: Returning two arrays..
in thread Returning two arrays..

I am sure this will be subject to debate, but I have learned that it is more efficent to return a reference then the whole thing.

For example, if you array is a million elements and you a million arrays, then that is alot to pass around. The size of that versus the size of one scalar (the reference) is obviously very different. Because of this, I have gotten in the habit of returning references to data structures, especially complex ones such as arrays or arrays.

Replies are listed 'Best First'.
Re: Re: Re: Re: Returning two arrays..
by chromatic (Archbishop) on Jan 14, 2002 at 00:44 UTC
    Beware of micro-optimizations, as they depend on non-obvious factors. At best, it's a wash. In a situation like this, I suspect it's actually less efficient to create a new array, push two references on it, take a reference to it, create a (single element) list, and return the list. The other alternative is to create a two element list and return that.

    Sure, there's one more element to push on the return stack and one more element to copy, if the caller uses the returned list, but you'll have to copy something in both cases, and if you return a reference to an array of references, you'll have to go through more levels of dereferencing.

    I don't know where it's more efficient to do it your way, but it probably doesn't converge until you're returning at least five or six references.