Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's better for performance, cell arrays of objects or heterogeneous arrays?

Suppose I have some classes foo < handle, and bar < foo, baz < foo, and maybe qux < foo. There are a couple ways I can store an array of these objects:

  • As a cell array: A = {foo bar baz qux} % A(1) would be a cell, A{1} gives me a foo object

  • Starting with R2011a, I can make foo <matlab.mixin.Heterogeneous, and then build an array directy: A = [foo bar baz qux] % A(1) directly gives me a foo object

The way I see it, from a maintenance perspective it would be better to use the second method rather than the first, this way it removes ambiguity about how to access A. Namely, when we need to dereference elements of the cell array (cell A(1) vs foo object A{1}, which lives inside A(1)).

But is there any kind of memory or performance penalty (or benefit) to using one syntax vs the other?

like image 642
Dang Khoa Avatar asked Dec 09 '14 17:12

Dang Khoa


People also ask

What are the advantages of cell array over normal array in Matlab?

Cell arrays contain data in cells that you access by numeric indexing. Common applications of cell arrays include storing separate pieces of text and storing heterogeneous data from spreadsheets. For example, store temperature data for three cities over time in a cell array.

Are Matlab cell arrays slow?

I've found that the performance of cell Arrays, multi-dimensional arrays, and structure arrays are all far slower than the use of multiple independent variables.

What are heterogeneous arrays?

A heterogeneous array is an array of objects that differ in their specific class, but all objects derive from or are instances of a common superclass. The common superclass forms the root of the hierarchy of classes that you can combine into heterogeneous arrays.

What is the difference between a cell array and an array in Matlab?

Array = a single variable (of any data type) that contains multiple content elements. Cell array = a specific type of array in MATLAB; an array of class cell. This is the "everything" container in MATLAB -- it's essentially a meta data type, or a "container" data type. You can put anything inside a cell.


1 Answers

I did a small experiment (source) on the memory and running time of the cell array, containers.Map and a Heterogeneous array. In my method I preallocated each array with N=65535 elements (the max array size for Map and Heterogeneous array), then began assigning each element a uint32, and measured the time and memory. My Heterogeneous Class was a simple class with a single public property, and a constructor which assigned that property. The containers.Map had uint32 key/value pairs.

Maps took 9.17917e-01 seconds.
Cells took 5.81220e-02 seconds.
Heterogeneous array took 4.95336e+00 seconds.

**Name**     **Size**         **Bytes**       **Class**   
map          65535x1           112          containers.Map              
cellArr      65535x1           7602060      cell               
hArr         1x65535           262244       SomeHeterogeneousClass   

Immediately note that the size of the mapArray is not accurate. It is hidden behind the containers.Map class implementation, most likley the 112 bytes reported is the memory assigned to the map itself, excluding the data. I approximate the true size to be at minimum (112+65535*(sizeof(uint32)*2)) = 524392 bytes. This value is almost exactly double the hArr size, which makes me think it is quite accurate, since the map must store twice as much data (for key AND value) as the hArr.

The results are straightforward:

  • Time: cell Array < Map < Heterogeneous Array
  • Memory: Heterogeneous Array < Map < cell Array

I repeated the experiment with N=30 to test for small arrays, the results were similar. God only knows why cells take up so much memory and Heterogeneous arrays are so slow.

like image 75
Gouda Avatar answered Sep 24 '22 04:09

Gouda