Postby ConvertFromOldNGs » Fri Aug 07, 2009 1:22 pm
by Dean Cooper >> Tue, 12 Aug 2008 9:36:25 GMT
You're correct that isKindOf is generally used most often. However, hasInstance is useful if the object reference you're wanting to check has been deleted (e.g. in a delete notification) because it doesn't dereference (i.e. access) the object, so it can be used to test a reference even if the object it references is no longer valid.
isKindOf is also faster, though you have to be doing a lot of them in a short timeframe to really notice the difference. For example, the test script below does 2m iterations of each check. Over three runs (the first being after a restart of JADE) using a 6.2.15 database server and a fat client, it produce the following output:
isKindOf = 1 seconds
hasInstance = 3 seconds
isKindOf = 1 seconds
hasInstance = 3 seconds
isKindOf = 1 seconds
hasInstance = 3 seconds
So isKindOf is consistently the fastest type check.
testTypeChecks();
constants
Iterations : Integer = 2000000;
vars
n, startClock : Integer;
b : Boolean;
o : Object;
begin
o := ObjectiveArea.firstInstance;
startClock := app.clock;
foreach n in 1 to Iterations do
b := o.isKindOf(ObjectiveArea);
endforeach;
write "isKindOf = " & ((app.clock - startClock) div 1000).String & " seconds";
startClock := app.clock;
foreach n in 1 to Iterations do
b := ObjectiveArea.hasInstance(o);
endforeach;
write "hasInstance = " & ((app.clock - startClock) div 1000).String & " seconds";
end;
Dean.