Is an array of structs a waste of memory?

It seems wasteful when I look at the memory used vs data stored.

1 件のコメント

Jan
Jan 2013 年 11 月 12 日
Bump! This is a very important detail for efficient Matlab programming. +1

サインインしてコメントする。

 採用された回答

Doug Hull
Doug Hull 2011 年 1 月 18 日

0 投票

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]

1 件のコメント

Joan Puig
Joan Puig 2011 年 2 月 5 日
Would the use of objects make it better?

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeMatrix Indexing についてさらに検索

製品

質問済み:

2011 年 1 月 18 日

コメント済み:

Jan
2013 年 11 月 12 日

Community Treasure Hunt

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

Start Hunting!

Translated by