Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

Views expressed in these public forums are not endorsed by Drexel University or The Math Forum.


Math Forum » Discussions » Software » comp.soft-sys.matlab

Topic: Problem with memory usage (R2009a)
Replies: 11   Last Post: Oct 21, 2009 10:20 AM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
Steven Lord

Posts: 17,944
Registered: 12/7/04
Re: Problem with memory usage (R2009a)
Posted: Oct 21, 2009 10:20 AM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply


"Outspan" <borghiborghi@gmail.com> wrote in message
news:720c9267-219b-4960-b3cf-305240212df8@o36g2000vbl.googlegroups.com...
> On Oct 21, 1:10 pm, "Bruno Luong" <b.lu...@fogale.findmycountry>
wrote:
> > Outspan <borghibor...@gmail.com> wrote in message
> > <3685c1d5-be81-4845-bcf2-90ad72037...@p20g2000vbl.googlegroups.com>...
> >

> > > even though every single field of the struct is only 8 bytes in size.
> > > I hope that in its internal representation it's not replicating the
> > > field names 500K times...

> >
> > No, the internal fieldnames is not duplicated, other internal "things"
> > (matlab mxArray like) do.
> >
> > Drop using array of struct if you want to save memory and speed.
> >
> > Bruno

>
> So I should use a matrix instead? Obviously that's going to save
> space, but the code is going to get ugly if I ever have to load
> different kinds of data for the same record. I just don't understand
> why structs have to take up that much space.


Rather than using an array of structs (and incurring the struct header
overhead for each element of that array) you could use a struct containing
arrays (and incur the header overhead once.)

arrayOfStructs = repmat(struct('x', 1), 1, 1000);
for k = 1:1000
arrayOfStructs(k).x = k;
end
structOfArrays = struct('x', 1:1000);
whos arrayOfStructs structOfArrays

Now note there's a trade-off here -- if each element of the arrayOfStructs
array contains data about a patient in a doctor's office, for example, then
it will be easier to retrieve data about an individual patient by using
arrayOfStructs(patientNumber). In structOfArrays, you will need to index
into each field (possibly using STRUCTFUN) to retrieve a patient's complete
record. On the other hand, if x contains the height of each patient, it's
easy to extract that data from structOfArrays (using heights =
structOfArrays.x;) but slightly more difficult for arrayOfStructs (you need
to remember to use square brackets, as in heights = [arrayOfStructs.x];)

This article from Loren's blog talks a little bit about the differences
between an array of structs and a struct containing arrays.

http://blogs.mathworks.com/loren/2006/05/10/memory-management-for-functions-and-variables/

as well as this section of the documentation:

http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_prog/brh72ex-2.html#brh72ex-5

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ





Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.