What is better for performance, object cell arrays or heterogeneous arrays?

Suppose I have some classes foo < handle and bar < foo , baz < foo and possibly qux < foo . There are several ways to store an array of these objects:

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

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

As I can see, from a service point of view, it is better to use the second method, rather than the first, thus it eliminates the ambiguity regarding access to A Namely, when we need to play the elements of an array of cells (cell A(1) vs foo object A{1} , which lives inside A(1) ).

But is there any memory or performance (or advantage) for using one syntax against another?

+6
source share
1 answer

I did a little experiment ( source ) in the memory and operating time of an array of cells, containers. Map and heterogeneous array. In my method, I pre-allocated each array with N = 65535 elements (the maximum array size for the Map and Heterogenic array), then began to assign uint32 to each element and measure time and memory. My heterogeneous class was a simple class with a single public property and the constructor that assigned this property. There were uint32 key / value pairs in the .Map containers.

 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 

Note immediately that the size of mapArray is inaccurate. It is hidden behind the implementation of the container.Map class, most of which corresponds to 112 bytes, this is the memory assigned to the card itself, with the exception of data. I approximate the true size to at least (112 + 65535 * (sizeof (uint32) * 2)) = 524392 bytes. This value is almost twice the size of hArr, which makes me think that it is pretty accurate, since the card should store twice as much data (for the value of the AND key) as hArr.

The results are simple:

  • Time: array of cells <Map <Heterogeneous array
  • Memory: heterogeneous array <Map <array of cells

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

+1
source

Source: https://habr.com/ru/post/979344/


All Articles