objective c - NSSet containing objects with overwritten isEqual -
in class overwrite isequal
@interface myclass : nsobject @property (nonatomic, strong) nsstring * customid; @end
i overwrite isequal checks equality of customid
- (bool)isequal:(id)object { if ([object iskindofclass:[myclass class]]) { if (self.customid == nil) { return no; } return [self.customid isequal:[object customid]]; } return [super isequal:object]; }
now nsset
practically hash table, making fast check, if contains hash value... thats know
but, let imagine code
nsarray * instancestocheck = ...; nsarray * allinstances = ...; (myclass * instance in allinstances) { if ([instancestocheck containsobject:instance]) { // smth } }
i "optimize" 1 (use nsset membership testing)
nsarray * instancestocheck = ...; nsarray * allinstances = ...; nsset * instancestocheckasset = [nsset setwitharray:instancestocheck]; (myclass * instance in allinstances) { if ([instancestocheckasset containsobject:instance]) { // smth } }
does second code provide performance benefit @ (under assumption, there no duplicates in array created, , instancestocheck contains different pointers, of objects have same customid, making isequal==yes pointer comparison==no)?
when looked docs, found out, containsobject calls isequal, has iterate on objects anyway
what performance implications when using nsset objects, overwrite isequal? becomes nsset less effective then?
does second code provide performance benefit @ all
absolutely. array must cycle through array examining every object. set knows more or less instantly whether object contained, because hash table. indeed, sort of thing set for.
Comments
Post a Comment