function for greatest object in list less than or equal to a value

hh hh at livecode.org
Tue Oct 13 04:35:53 EDT 2015


@Craig
I also found the "sort" method faster than the "repeat" method on
a PPC. On intel, under several OSes, here always won the [1] below.

@Bernd
I didn't include your solution because pre-sorting wastes too
much time. Perhaps sorting could be included in your binary loop?

@Geoff
You could also try with a critical value near the "edges":
greatestLessThanRepeat(L,1) and
greatestLessThanRepeat(L,1000000000-1)
and then average your three runs?

From my tests
(referencing the list gives moreover a smallincrease in speed.
Empty list members are not handled, LC handles empty as zero):

** The simplest is the fastest **

[1] (contributed by P.M.B.)
function getMaxLessThan tList,maxVal
   repeat for each item i in tList
      if i < maxVal then put i & comma after outList
   end repeat
   return max(outList)
end getMaxLessThan

Even the clever idea of max-finding in the same loop

[2] (contributed by G.C.)
function greatestLessThanRepeat pList,V
   put empty into R
   repeat for each item i in pList
      if i < V and i > R then put i into R
   end repeat
   return R
end greatestLessThanRepeat

is *slightly* slower.

[3] (contributed by me)
function greatestMemberSmallerThan L,V
   put -10^15 into z[true]
   repeat for each item x in L
       put comma & x after z[x<v]
   end repeat
   return max( z[true] )
end greatestMemberSmallerThan

is slowest (close to the above only if the smaller-than-list is nearly
the full input list).
And it will be of about the same speed only if the smallest member of
the list that is greater v (x>v) is also asked for, with the same v.

Hermann



More information about the use-livecode mailing list