Data Saving - Storage Efficiency

3 views (last 30 days)
Gavin
Gavin on 5 Apr 2013
Just out of interest: I was having a discussion with a friend today and were trying to decide if there are actually any advantages of storing data (say, for example, error Flags) as integers or as logicals.
For example: Say I wanted to determine if an array of data had too high an RMS value. I could store that as a simple (1/0) from True/False...or I could store it as an integer (1/2/3...) etc. depending on how much higher than the "maximum" or "threshold" RMS value is.
Theoretically speaking, a logical would only require one bit of memory. But, all memory address are usually 32 or 64 bits anyway. So, would storing it as a logical have any obvious or immediate memory advantage over storing it as, say, int4?
Gav

Answers (1)

Cedric
Cedric on 5 Apr 2013
Edited: Cedric on 5 Apr 2013
>> f_dbl = 1 ;
>> f_log = true ;
>> whos
Name Size Bytes Class Attributes
f_dbl 1x1 8 double
f_log 1x1 1 logical
Logicals are stored on one byte each. You can manage flags stored as bits in integer variables, but this is likely to induce some overhead due to bits manipulation.
Now it's faster to store logicals than doubles, because it takes 8 times less room in memory; also, accessing them 8 logicals at a time is faster than 1 double at a time..
>> tic; F_dbl = zeros(1e4); toc
Elapsed time is 0.260210 seconds.
>> tic; F_log = false(1e4); toc
Elapsed time is 0.032855 seconds.
>> 0.260210 / 0.032855 % Should be close to factor 8.
ans =
7.9200
>> whos % As seen previously, factor on size is 8.
Name Size Bytes Class Attributes
F_dbl 10000x10000 800000000 double
F_log 10000x10000 100000000 logical
So the/an answer is that logicals (bytes, and not bits) are likely to be your best option (using 1GB RAM for storing 1e9 flags instead of 8GB makes a significant difference, even without thinking about access time), unless you want to cope with the overhead induced by bits manipulation for fully optimizing memory usage.

Products

Community Treasure Hunt

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

Start Hunting!