Parent of Target
Richard Gaskin
ambassador at fourthworld.com
Thu Aug 10 15:10:11 EDT 2017
Ali Lloyd wrote:
> Richard wrote
>> Jacque recently showed me the speed difference between explicitly
>> writing out the element of an object reference:
>>
>> get the width of btn 1 of cd 2 of stack "MyStack"
>>
>> ...vs other forms like long IDs:
>>
>> put the long is of btn 1 of cd 2 of stack "MyStack" into t5Obj
>> get teh width of tObj
>>
>> The latter is much slower, yet long IDs are so good to work with.
>
> The only reason this is true is that in the second case you are
> resolving the object twice.
Indeed - a very poor example on my part. Also "teh" is not a recognized
token. :)
> It is not true in general - the second time the long id is used it
> will use the id cache which is constant time, whereas "button n"
> is O(n). Try benchmarking repeated use of the stored long id vs the
> number version, especially if the numbers are large.
>
> So it's horses for courses. If it's a one-shot object access then the
> number form is faster. For repeated use, get the long id.
Sounds good, but the benefits of long ID caching have been difficult for
me to measure.
I was exploring this a while back in response to this forum thread:
http://forums.livecode.com/viewtopic.php?f=7&t=29555
Of course object ref performance matters most when you need to traverse
a lot of objects or refer to objects frequently, so my test attempts to
do both:
I made a simple stack with one button containing the script below, and
one group containing 100 buttons.
The script uses three different methods to obtain a property value from
every object in the group, through 100 iterations, for a total of 10,000
accesses with each method.
When I'd posted earlier the property being obtained was the short ID,
which seems a reasonable reflection of time needed to obtain an
arbitrary property value.
After reading your post I changed that to long ID, but see little
difference in relative performance.
How might I measure the benefits of long ID caching?
--=================================================--
local sObjRefsA
on mouseUp
put 100 into n
put (the number of btns of grp 1) * n into tIterations
--------------
put the millisecs into t
repeat n
put ObjRef1() into r1
end repeat
put the millisecs - t into t1
--
put the millisecs into t
repeat n
put ObjRef2() into r2
end repeat
put the millisecs - t into t2
------------------
-- Last test requires that we first obtain the object refs to
-- measure the benefits of long ID caching:
repeat with i = 1 to the number of btns of grp 1
put the long id of btn i of grp 1 into sObjRefsA[i]
end repeat
--
put the millisecs into t
repeat n
put ObjRef3() into r3
end repeat
put the millisecs - t into t3
--------------------
set the numberformat to "0.0000"
put t1/tIterations &" - Method 1 - full expression by number " &cr \
& t2/tIterations &" - Method 2 - mixed expression" &cr \
& t3/tIterations &" - Method 3 - long ID only" &cr \
& (r1 = r3)
end mouseUp
-- Ordinal references all the way down:
function ObjRef1
put the number of btns of grp 1 into tNumBtns
repeat with x = 1 to tNumBtns
put the long id of btn x of grp 1 of cd 1 of stack \
"objref test" &cr after s
end repeat
return s
end ObjRef1
-- Mix of ordinal button with group long ID:
function ObjRef2
put the long id of grp 1 into tGrpObj
put the number of btns of tGrpObj into tNumBtns
repeat with x = 1 to tNumBtns
put the long id of btn x of tGrpObj &cr after s
end repeat
return s
end ObjRef2
-- Long ID:
function ObjRef3
put the number of btns of grp 1 into tNumBtns
repeat with x = 1 to tNumBtns
put the long id of sObjRefsA[x] &cr after s
end repeat
return s
end ObjRef3
--
Richard Gaskin
Fourth World Systems
Software Design and Development for the Desktop, Mobile, and the Web
____________________________________________________________________
Ambassador at FourthWorld.com http://www.FourthWorld.com
More information about the use-livecode
mailing list