Results of Offset handler speed tests
Results of Offset handler speed tests
- Subject: Results of Offset handler speed tests
- From: Greg Back <email@hidden>
- Date: Mon, 08 Oct 2001 16:20:32 -0400
on Mon, 8 Oct 2001 03:02:46 +0100, Nigel Garvey at
email@hidden wrote:
>
>
Greg Back wrote on Sun, 07 Oct 2001 12:57:39 -0400:
>
>
> Hi all-
>
>
>
> Is there any more practical (read "shorter") way to find where in a list a
>
> certain item can be found.
>
>
>
> to findPosition(thisItem, thislist)
>
> if thisItem is in thislist then
>
> repeat with i from 1 to (count thislist)
>
> if item i of thislist is thisItem then
>
> set j to i
>
> exit repeat
>
> end if
>
> end repeat
>
> else
>
> set j to "Not in list"
>
> end if
>
> return j
>
> end findPosition
>
>
That's about the best way there is for short lists. Last week, I wrote a
>
binary search handler for this purpose, which, although regrettably not
>
shorter, might be faster with longer lists:
You're right, (A LOT FASTER)...speed test results follow
>
>
on listOffset of theItem into theList
>
if theItem is not in theList then return 0
>
set {L, R} to {1, count theList}
>
repeat
>
if theItem is item L of theList then return L
>
set M to (L + R) div 2
>
if theItem is in (items (L + 1) thru M of theList) then
>
set {L, R} to {L + 1, M}
>
else
>
set L to M + 1
>
end if
>
end repeat
>
end listOffset
>
>
NG
Here's the script I used to test the speed...
where x is a list of integers 1-1000 (too large to post)
======================================================================
set time1 to the ticks
--repeat 100 times
findPosition(1, x)
--findPosition(971, x)
--end repeat
set time2 to the ticks
--repeat 100 times
listOffset(1, x)
--listOffset(971,x)
--end repeat
set time3 to the ticks
log (time2 - time1)
log (time3 - time2)
to findPosition(thisItem, thislist)
set j to ""
if thisItem is in thislist then
repeat with i from 1 to (count thislist)
if item i of thislist is thisItem then
set j to i as string
exit repeat
end if
end repeat
else
set j to "Not in list"
end if
return j
end findPosition
on listOffset(theItem, theList)
if theItem is not in theList then return 0
set {L, R} to {1, count theList}
repeat
if theItem is item L of theList then return L
set M to (L + R) div 2
if theItem is in (items (L + 1) thru M of theList) then
set {L, R} to {L + 1, M}
else
set L to M + 1
end if
end repeat
end listOffset
======================================================================
[formatted using ScriptToEmail - gentle relief for mailing list pains]
[
http://files.macscripter.net/ScriptBuilders/ScriptTools/ScriptToEmail.hqx]
And the Results--(iMac G3 400 mHz, AS 1.4 SE 1.4.3 )
Also note that both only look for the FIRST occurence in the list.
number |1 |971 |1 run 100 times
_______________|________|__________|_______________
findPosition |4 or 5 |227-237 |98-105
listOffset |5 or 6 |7-9 |91-102
Overall, Nigel's is faster, especially in (items near the end of) long
lists.
My thoughts:
***Defeated once again, darn***
***Must learn not to try to do better than people who have been scripting
for more than 4 months (like me)***
I consider myself an intermediate scripter, not all-knowing, but with a
little experience. Every script is a stillnew experience for me
--
Greg