gpt4 book ai didi

matlab - MATLAB 中的映射有多少字节?

转载 作者:行者123 更新时间:2023-12-05 02:24:01 34 4
gpt4 key购买 nike

我有一个 map称为 res_Map,包含一组不同大小的数组。我想找到用于存储 res_Map 的总内存。

正如您在下面看到的,看起来 res_Map 几乎不占用内存,而 res_Map 中的各个元素却占用内存。

res_1 = res_Map(1);
>> whos
Name Size Bytes Class Attributes

res_1 118x100 94400 double
res_Map 11x1 112 containers.Map

有谁知道如何找到用于存储 res_Map 的实际内存?我在 documentation 中找不到任何关于此的信息.

最佳答案

containers.Map 对象是一个与其他任何对象一样的 Matlab 对象。在引擎盖下,这些被实现为带有一些额外访问控制和函数映射的 Matlab 结构。

您可以使用 struct 命令强制 Matlab 向您显示原始结构。这会引发警告,因为通常不推荐这样做。然而,类的结构 View 显示了完整的内容,并准确地反射(reflect)在 whos 调用中。

部分示例代码如下:

%Initialize map and add some content
res_Map = containers.Map;
for ix = 1:1000
res_Map(sprintf('%05d',ix)) = ix;
end

%Look at the memory used by the map
disp('Raw who: always 112 Bytes for Map')
whos('res_Map')

%Force the map into a structure, and look at the contained memory
mapContents = struct(res_Map);
disp('Look at the size of the map contents, reflect true size')
whos('res_Map','mapContents')


%Add additional contents and check again.
for ix = 1001:2000
res_Map(sprintf('%05d',ix)) = ix;
end
mapContents = struct(res_Map);
disp('Look at the size of the map contents, reflect true size')
whos('res_Map','mapContents')

上述脚本的结果(删除警告消息后)如下所示:

Raw who:  always 112 Bytes for Map
Name Size Bytes Class Attributes
res_Map 1000x1 112 containers.Map

Look at the size of the map contents, reflect true size
Name Size Bytes Class Attributes
mapContents 1x1 243621 struct
res_Map 1000x1 112 containers.Map


Look at the size of the map contents, reflect true size
Name Size Bytes Class Attributes
mapContents 1x1 485621 struct
res_Map 2000x1 112 containers.Map

关于matlab - MATLAB 中的映射有多少字节?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16902910/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com