Is there a "signature" for objects instances of subclasses of handle?
1 次查看(过去 30 天)
显示 更早的评论
Dear all,
obj1 = MyClass() ;
n = 1e6 ;
obj2 = obj1 ;
tic ; for k = 1 : n, isequal( obj2, obj1 ) ; end, toc
% -> Elapsed time is 2.107606 seconds.
obj3 = MyClass() ;
tic ; for k = 1 : n, isequal( obj3, obj1 ) ; end, toc
% -> Elapsed time is 68.857936 seconds.
If objects had an accessible signature (e.g. unique unint64) or any equivalent information/mechanism, even the first loop would not take more than a few milliseconds, hence my question.
Thanks,
Cedric
NOTES
- I didn't find any equivalent information in objects' metaclass structs.
- ...
0 个评论
回答(1 个)
Shruti Sapre
2015-9-3
Hi Cedric,
I understand that you want to be able to compare two objects are equivalent (if they reference the same instance). I was unable to find a “signature” for the object, but it could be possible to implement this. Since “isequal” is slow for your class, you could maybe have a counter in your class that increments itself when an object is created, and compare the counter when determining if the two instances are the same. You could even have your own “isequal” function which does this.
classdef MyClass < handle
properties
counter
end
methods
function obj=MyClass(obj)
persistent c;
if isempty(c)
c=0;
else
c=c+1;
end
obj.counter=c;
end
function display(obj)
disp(obj.counter);
end
function res=isequal(obj1,obj2)
if obj1.counter==obj2.counter
res=true;
else
res=false;
end
end
end
end
I’m not sure if this would be faster, but it is an alternative approach.
Hope this helps!
-Shruti
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Software Development Tools 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!