ISSHARED determines data sharing status among workspace variables

版本 3.0.0.0 (51.8 KB) 作者: James Tursa
ISSHARED determines data sharing status among workspace variables
46.0 次下载
更新时间 2018/5/21

查看许可证

ISSHARED determines if a variable is sharing data with other variables. This sharing takes place automatically in MATLAB behind the scenes, and normally the user need not pay attention to it. However, if the user is working with large variables, it can be a huge benefit to understand how this sharing is taking place and what actions can cause shared data copies vs deep data copies in order to write code that better manages memory and timing. The types of sharing that can be detected by this mex routine are:

Shared Data Copy --> Separate header but data pointers are shared

Reference Copy --> Variable header & data pointers are shared.
Note: Reference copies can either be direct
or through a shared "parent". See examples.
E.g., struct field elements and cell elements
are often shared this way.

Looks for sharing among ordinary workspace variables, classdef object public properties, and function handle embedded data items. This mex routine cannot always tell when empty variables are shared reference copies of each other. E.g.,
>> x = 1:3
x =
1 2 3
>> y = x
y =
1 2 3
>> isshared(x,y)
ans =
1
>> y(1) = 4
y =
4 2 3
>> isshared(x,y)
ans =
0
In the above example, the assignment y = x caused the variable y to be created such that it was sharing the same data memory as x. But when we subsequently changed one of the elements of y, suddenly the real data memory of y points to a different place. This is known as copy-on-write. MATLAB does not make a deep data copy (i.e., copying the data to a brand new memory location) until it needs to. It didn't need to for the initial y = x assignment, but it did need to when one of the elements of y was changed. ISSHARED can be used to show this type of sharing for all of the variables in the current workspace.
In addition to simply reporting if input arguments are shared, ISSHARED can generate sharing name lists for single variables, or for all of the variables in the current workspace. E.g.,
[N,M] = isshared;
N = a vector containing the number of variables in each sharing group that were detected
M = a cell array of cell array of char strings containing the names of the sharing variables

引用格式

James Tursa (2024). ISSHARED determines data sharing status among workspace variables (https://www.mathworks.com/matlabcentral/fileexchange/65861-isshared-determines-data-sharing-status-among-workspace-variables), MATLAB Central File Exchange. 检索时间: .

MATLAB 版本兼容性
创建方式 R2011a
兼容任何版本
平台兼容性
Windows macOS Linux
类别
Help CenterMATLAB Answers 中查找有关 Workspace Variables and MAT-Files 的更多信息
标签 添加标签

Community Treasure Hunt

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

Start Hunting!
版本 已发布 发行说明
3.0.0.0

Updated for R2018a

2.0.0.0

Updated for 64-bit and later versions of MATLAB. Added classdef object public property and function handle data capability.

1.0.0.0