what is the extra data in a dictionary?

2 views (last 30 days)
min lee
min lee on 9 Apr 2024
Commented: Stephen23 on 9 Apr 2024
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 Comments
Manikanta Aditya
Manikanta Aditya on 9 Apr 2024
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 on 9 Apr 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."

Sign in to comment.

Answers (0)

Categories

Find more on Dictionaries in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!