Path: news.mathworks.com!not-for-mail
From: Loren Shure <loren@mathworks.com>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Pass by Reference?
Date: Fri, 02 Jun 2006 07:24:29 -0400
Organization: The MathWorks, Inc.
Lines: 87
Message-ID: <e5p75d$5g9$2@fred.mathworks.com>
References: <ef386cf.-1@webcrossing.raydaftYaTP> <447f5e07$0$18308$ba4acef3@news.orange.fr> <e5osdd$gg7$1@newsreader1.utanet.at>
NNTP-Posting-Host: shurel.dhcp.mathworks.com
Mime-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-15; format=flowed
Content-Transfer-Encoding: 7bit
X-Trace: fred.mathworks.com 1149247469 5641 144.212.219.39 (2 Jun 2006 11:24:29 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Fri, 2 Jun 2006 11:24:29 +0000 (UTC)
User-Agent: Thunderbird 1.5.0.2 (Windows/20060308)
In-Reply-To: <e5osdd$gg7$1@newsreader1.utanet.at>
Xref: news.mathworks.com comp.soft-sys.matlab:353071



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/