• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Determining item number matching "x" in a list
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Determining item number matching "x" in a list


  • Subject: Re: Determining item number matching "x" in a list
  • From: Emmanuel <email@hidden>
  • Date: Thu, 13 Mar 2003 23:41:58 +0100

At 2:31 PM -0500 13/03/03, Steve Cunningham wrote:
Emmanuel <email@hidden> wrote:

on IndexOfItem(theItem, theList)
set {savedTID, text item delimiters} to {text item delimiters, return}
set theList to return & theList & return
set text item delimiters to savedTID
set theOffset to offset of (return & theItem & return) in theList
if theOffset is 0 then return 0
-1 + (count paragraphs of (text 1 thru theOffset of theList))
end IndexOfItem

[...]

on getIndex(theItem, theList)
set theIndex to 0
repeat with nn from 1 to count of theList
if theItem = item nn of theList then
set theIndex to nn
exit repeat
end if
end repeat
return theIndex
end getIndex

I ran this 500 times and Emmanuel's script 500 times for the same item
and the same list.

Mine completes in 242 ms and Emmanuel's in 1385 ms (on my Lombard).
[...]
So here is my challenge: what is the *fastest* way to do this job?

I tested with a list of 20 short strings, for all items successively. (2 x 20 runs, thus). I eliminated the largest value in each list of results (each list had an isolated anomalous value, that's OSX spinning the disk). I repeated 100 times, not 500. Units are microseconds.

"getindex" needs from 125 to 2789, average 1354, st dev 763
"IndexOfItem" needs from 774 to 1134, average 850, st dev 88

If the item is 6th or below, "getindex" is better, for the 7th or above "IndexOfItem" is faster.

(You improve significantly IndexOfItem's performances if you can forget about resetting tids).

Logically, thus, a faster algorithm would be hybrid: you would use the loop method to test whether the item is in the very beginning of the list, then you would fire the string method on the rest of the list. With the short list I used (20 items), the additional lines of script required to joint the two methods make the hybrid algorithm ineffective.

My faster proposition so far (10% better than previously):

on IndexOfItem(theItem, theList)
set text item delimiters to return
set theList to return & theList & return
try
-1 + (count (paragraphs of (text 1 thru (offset of (return & theItem & return) in theList) of theList)))
on error
0
end try
end IndexOfItem

Emmanuel
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.

References: 
 >Re: Determining item number matching "x" in a list (From: Steve Cunningham <email@hidden>)

  • Prev by Date: Re: reading a file >32Kb
  • Next by Date: Re: Code for a circle
  • Previous by thread: Re: Determining item number matching "x" in a list
  • Next by thread: Re: Determining item number matching "x" in a list
  • Index(es):
    • Date
    • Thread