Thread Subject: Pass by Reference?

Subject: Pass by Reference?

From: Brandon Aubie

Date: 1 Jun, 2006 16:14:57

Message: 1 of 5

Hi,

Is there a "proper" way to pass objects by reference? We need to
pass a large object around NUMEROUS times (ie, >10000) and it's
very slow. Passing by reference would speed this up tremendously.

Thanks

Subject: Pass by Reference?

From: Peter Boettcher

Date: 1 Jun, 2006 17:20:58

Message: 2 of 5

"Brandon Aubie" <brandon.aubie@TAKETHISOUTelecore.com> writes:

> Hi,
>
> Is there a "proper" way to pass objects by reference? We need to
> pass a large object around NUMEROUS times (ie, >10000) and it's
> very slow. Passing by reference would speed this up tremendously.

Simply passing it around (and accessing it) is not slow. No copy is
done, and pass-by-reference is indeed the internal implementation. If
you start modifying the object, then the data is copied.

--
Peter Boettcher <boettcher@ll.mit.edu>
MIT Lincoln Laboratory
MATLAB FAQ: http://www.mit.edu/~pwb/cssm/

Subject: Pass by Reference?

From: CRet

Date: 1 Jun, 2006 23:36:20

Message: 3 of 5

HI,

As long as you only have one object to pass. Why not simply declare it as
global ?

I know, global variables are not recommended but it will greatly simplify
argument passing.
This is only a suggestion. I will not be reponsible of any lost of data,
property damage, personal injury, lost of time in development or maintenance
of the code. Even be involve in the future problematic evolution of your
application since I forcast that you will in a close futur have two or three
of this large object to handel simultaneously.

Remember, integrety of your aobject will be difficult to maintain, since all
functions or scripts which declare it as global may modify or delete it
expectively or unexpectively. This make sametime debugging really hard for
large project. If you are leading a developer team, be sure co-workers
respect naming rules and access policy to your base workspace and disk paths
!

If some one here or there ask me if I give you this advice. I will denie. I
will say that I never use any global variable, or I only have one time, a
long time ago, on low RAM system, few disk space networks, no GUI, only
command line intructions setup, on slow micro-WAX machines, reading large
data set (concept of objects was not establish) from kilometric magnetic
tapes, unsing MATLAB ver2 or 3...
At this time, global variable was the only change we have to run the
project.

Hope this help.

C.Ret

P.S.: "very slow" means how many days of computations ? I remenber that
former project when the first steps to prepare the large data set from raw
acquisitions was about 72h.

    If "very slow" means 1 or 2 mins, there is perhaps no benefit to
investigate time in modifying and debugging your actual code. Think at the
time gained at future evolutions or developments of your already well
structurated data object and code compared to difficult evolution of an
unstructurated code using global workspace.


"Brandon Aubie" <brandon.aubie@TAKETHISOUTelecore.com> wrote in
news:ef386cf.-1@webcrossing.raydaftYaTP...
> Hi,
>
> Is there a "proper" way to pass objects by reference? We need to
> pass a large object around NUMEROUS times (ie, >10000) and it's
> very slow. Passing by reference would speed this up tremendously.
>
> Thanks


Subject: Pass by Reference?

From: Michael Salloker

Date: 2 Jun, 2006 10:20:59

Message: 4 of 5

CRet schrieb:
> HI,
>
> As long as you only have one object to pass. Why not simply declare it as
> global ?
>
> I know, global variables are not recommended but it will greatly simplify
> argument passing.
> This is only a suggestion. I will not be reponsible of any lost of data,
> property damage, personal injury, lost of time in development or maintenance
> of the code. Even be involve in the future problematic evolution of your
> application since I forcast that you will in a close futur have two or three
> of this large object to handel simultaneously.
>
> Remember, integrety of your aobject will be difficult to maintain, since all
> functions or scripts which declare it as global may modify or delete it
> expectively or unexpectively. This make sametime debugging really hard for
> large project. If you are leading a developer team, be sure co-workers
> respect naming rules and access policy to your base workspace and disk paths
> !
>
> If some one here or there ask me if I give you this advice. I will denie. I
> will say that I never use any global variable, or I only have one time, a
> long time ago, on low RAM system, few disk space networks, no GUI, only
> command line intructions setup, on slow micro-WAX machines, reading large
> data set (concept of objects was not establish) from kilometric magnetic
> tapes, unsing MATLAB ver2 or 3...
> At this time, global variable was the only change we have to run the
> project.
>
> Hope this help.
>
> C.Ret
>
> P.S.: "very slow" means how many days of computations ? I remenber that
> former project when the first steps to prepare the large data set from raw
> acquisitions was about 72h.
>
> If "very slow" means 1 or 2 mins, there is perhaps no benefit to
> investigate time in modifying and debugging your actual code. Think at the
> time gained at future evolutions or developments of your already well
> structurated data object and code compared to difficult evolution of an
> unstructurated code using global workspace.
>
>
> "Brandon Aubie" <brandon.aubie@TAKETHISOUTelecore.com> wrote in
> news:ef386cf.-1@webcrossing.raydaftYaTP...
>
>>Hi,
>>
>>Is there a "proper" way to pass objects by reference? We need to
>>pass a large object around NUMEROUS times (ie, >10000) and it's
>>very slow. Passing by reference would speed this up tremendously.
>>
>>Thanks
>
>
>

I would not recommend global variables, the reasons are posted in this
forum often, and are discussed much.

An alternative approach is the use of nested functions. A nested
function can access the variables of all functions in which it is
nested. So ther is no need to pass any parameter.

Michael

Subject: Pass by Reference?

From: Loren Shure

Date: 2 Jun, 2006 07:24:29

Message: 5 of 5

Michael Salloker wrote:
> CRet schrieb:
>> HI,
>>
>> As long as you only have one object to pass. Why not simply declare it as
>> global ?
>>
>> I know, global variables are not recommended but it will greatly simplify
>> argument passing.
>> This is only a suggestion. I will not be reponsible of any lost of data,
>> property damage, personal injury, lost of time in development or
>> maintenance
>> of the code. Even be involve in the future problematic evolution of your
>> application since I forcast that you will in a close futur have two or
>> three
>> of this large object to handel simultaneously.
>>
>> Remember, integrety of your aobject will be difficult to maintain,
>> since all
>> functions or scripts which declare it as global may modify or delete it
>> expectively or unexpectively. This make sametime debugging really hard
>> for
>> large project. If you are leading a developer team, be sure co-workers
>> respect naming rules and access policy to your base workspace and disk
>> paths
>> !
>>
>> If some one here or there ask me if I give you this advice. I will
>> denie. I
>> will say that I never use any global variable, or I only have one time, a
>> long time ago, on low RAM system, few disk space networks, no GUI, only
>> command line intructions setup, on slow micro-WAX machines, reading
>> large
>> data set (concept of objects was not establish) from kilometric magnetic
>> tapes, unsing MATLAB ver2 or 3...
>> At this time, global variable was the only change we have to run the
>> project.
>>
>> Hope this help.
>>
>> C.Ret
>>
>> P.S.: "very slow" means how many days of computations ? I remenber that
>> former project when the first steps to prepare the large data set from
>> raw
>> acquisitions was about 72h.
>>
>> If "very slow" means 1 or 2 mins, there is perhaps no benefit to
>> investigate time in modifying and debugging your actual code. Think at
>> the
>> time gained at future evolutions or developments of your already well
>> structurated data object and code compared to difficult evolution of an
>> unstructurated code using global workspace.
>>
>>
>> "Brandon Aubie" <brandon.aubie@TAKETHISOUTelecore.com> wrote in
>> news:ef386cf.-1@webcrossing.raydaftYaTP...
>>
>>> Hi,
>>>
>>> Is there a "proper" way to pass objects by reference? We need to
>>> pass a large object around NUMEROUS times (ie, >10000) and it's
>>> very slow. Passing by reference would speed this up tremendously.
>>>
>>> Thanks
>>
>>
>>
>
> I would not recommend global variables, the reasons are posted in this
> forum often, and are discussed much.
>
> An alternative approach is the use of nested functions. A nested
> function can access the variables of all functions in which it is
> nested. So ther is no need to pass any parameter.
>
> Michael
>

I agree that nested functions might provide a good way to go - depending
on your code base and how many people edit it. See this post:
http://blogs.mathworks.com/loren/?p=36 for more information about how
MATLAB handles variables and uses memory.


--Loren
http://blogs.mathworks.com/loren/

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

rssFeed for this Thread
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com