what is the extra data in a dictionary?

8 次查看(过去 30 天)
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 个评论
Manikanta Aditya
Manikanta Aditya 2024-4-9
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 2024-4-9
"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."

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Dictionaries 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by