Is an array of structs a waste of memory?

17 views (last 30 days)
It seems wasteful when I look at the memory used vs data stored.
  1 Comment
Jan
Jan on 12 Nov 2013
Bump! This is a very important detail for efficient Matlab programming. +1

Sign in to comment.

Accepted Answer

Doug Hull
Doug Hull on 18 Jan 2011
The following example was posted to the newsgroup:
I've discovered to my horror that structs take up an obscene amount of overhead (I'm running version 5.3.1.29215a (R11.1) on a Dec ALPHA). I have a set of 10,242 observations, each consisting of 3+13=16 fields, which have 3*27 + 1*13 = 94 values. So the total size in bytes should be 10,242 * 94 * 8 bytes/double = 7,701,984.
I have this stored in a 1 x 10242 data structure, and when I issue the whos command, it tells me that the data now takes up 27,367,136 bytes!
Professor Cris Luengo answers:
My guess would be that a structure contains MATLAB arrays. Each array has some overhead, like data type, array sizes, etc. In your second implementation (index using data.latitude(observation)), there are 10,242 times less arrays allocated. Note that in your data, for each observation, you have 13 arrays with one value. I don't know how large the matrix header exactly is, but it is a waste putting only a single value in it!
I think Cris has hit it exactly. Every MATLAB matrix has an overhead of ~100 bytes, even matrices with a single element. In this example, there are 16 fields * 10242 elements = 163872 matrices. Each one of these matrices adds an additional 100 bytes, for 16.4Mbytes in pure overhead. This still comes up a little short of the amount reported, but it is fairly close.
It is much more efficient, both for storage and computation, to use a struct of arrays rather than an array of structs.
[From the MATLAB FAQ of Ancient Times]

More Answers (0)

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!