Re: Functions vs. parameters
Jay Turley writes:
> If I pass a parameter to a function, and the same parameter
> is returned by that function, what is the overhead cost associated
> with it, i.e. am I wasting time copying the object into memory?
> Is a copy of that object actually sent? I would assume so, because
> if a function acts on a parameter and doesn't return it, the original
> copy is unchanged.
The way things work internally is pretty simple.
Euphoria only goes to the trouble of making a copy of a
sequence when it's really necessary.
If you pass a sequence s to a function foo:
s = {1,2,3,{4,5,6,7}}
x = foo(s)
only a pointer to s is passed. The elements
contained in s are *not* copied. Suppose foo() looks like:
function foo(sequence t)
return t[1]
end function
then while foo() is executing, t and s point at the same data in memory.
Since t is not modified, there is never any reason to make
a copy. If however you have something like:
function foo(sequence t)
t[3] = 99
return t
end function
then at the point that t[3] is set to 99 it will no longer
be possible for t and s to point at the same data
in memory - the interpreter will create a new
sequence in memory {1,2,99,{4,5,6,7}} and t will point to it,
while s continues to point at the original sequence.
Note that the {4,5,6,7} element of s will *not* be copied.
Only the "top-level" elements need to be copied.
A single copy of {4,5,6,7} will be pointed to from s[4] and t[4].
Regards,
Rob Craig
Rapid Deployment Software
http://members.aol.com/FilesEu/
|
Not Categorized, Please Help
|
|