what is the extra data in a dictionary?

When a matrix is converted to a dictionary, the memory assumption increases as demonstrated below. What is the extra data in a dictionary?
>> b = [1:10; 1:10];
>> whos b
Name Size Bytes Class Attributes
b 2x10 160 double
>> a = dictionary(b(1,:), b(2,:));
>> whos a
Name Size Bytes Class Attributes
a 1x1 304 dictionary

3 commentaires

It seems that the extra data is always 144 bytes
>> b = [1:10000; 1:10000];
>> a = dictionary(b(1,:), b(2,:));
>> whos b
Name Size Bytes Class Attributes
b 2x10000 160000 double
>> whos a
Name Size Bytes Class Attributes
a 1x1 160144 dictionary
The extra data in a dictionary compared to a matrix in your example is likely due to the overhead of the dictionary data structure.
A dictionary, unlike a matrix, needs to store additional information such as keys and possibly some metadata for each key-value pair. This extra information results in an increase in memory usage.
In your example, the dictionary a uses 144 bytes more than the matrix b. This additional memory is consistent across different sizes of the matrix and dictionary, suggesting that it’s a fixed overhead cost for using the dictionary data structure.
Stephen23
Stephen23 le 9 Avr 2024
"A dictionary, unlike a matrix, needs to store additional information"
All arrays (even numeric ones) store a header with array meta-information, such as the type, dimensions, and attributes. This is explained in the MATLAB documentation: "MATLAB arrays (implemented internally as mxArrays) require room to store meta information about the data in memory, such as type, dimensions, and attributes. This takes about 104 bytes per array. This overhead only becomes an issue when you have a large number (e.g., hundreds or thousands) of small mxArrays (e.g., scalars). The whos command lists the memory used by variables, but does not include this overhead."

Connectez-vous pour commenter.

Réponses (0)

Catégories

Tags

Question posée :

le 9 Avr 2024

Commenté :

le 9 Avr 2024

Community Treasure Hunt

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

Start Hunting!

Translated by