Is there a "signature" for objects instances of subclasses of handle?
조회 수: 2 (최근 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에서 Enumerations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!