Is there a "signature" for objects instances of subclasses of handle?
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Antworten (1)
Shruti Sapre
am 3 Sep. 2015
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
1 Kommentar
Siehe auch
Kategorien
Mehr zu Software Development Tools finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!