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

Geoff Canyon gcanyon at gmail.com
Tue Oct 13 10:07:08 EDT 2015


On Tue, Oct 13, 2015 at 4:35 AM, hh <hh at livecode.org> wrote:

> 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
>

This returns 0 if there is no valid result, rather than empty. I modified
it like this to get the correct result in that case:

function getMaxLessThan tList,maxVal
   repeat for each item i in tList
      if i < maxVal then put i & comma after outList
   end repeat
   if outList is empty then return empty else return max(outList)
end getMaxLessThan

I then created three test cases, searching for a small upper limit, a mid
upper limit, and a high upper limit, as shown in the code. I saw
variability in the results, so I wrote the test to run 10 times and take
the average. Here are the results, running on a MB Pro in LC 6.7.3:

Run Count: 10

Test ID: 1 Looking for greatest value < 500000000
sort 0.948062 499999481
repeat 0.347538 499999481
PMB 0.354209 499999481

Test ID: 2 Looking for greatest value < 5
sort 0.950574
repeat 0.225004
PMB 0.212485

Test ID: 3 Looking for greatest value < 999999995
sort 0.920444 999999683
repeat 0.442339 999999683
PMB 0.527431 999999683

In single runs I've seen PMB beat repeat on test 1. PMB is consistently
slightly faster on test 2, and consistently slower (more than the
difference on test 2) on test 3. Unless there is a difference based on
hardware or LC version, I think it comes down to taste for which to use
between the two. I never expected the sort version to be even as close as
it was, but for most purposes even it would seem to be an okay choice.

Code used:

on mouseUp
   repeat 1000000
      put random(1000000000),"" after L
   end repeat

   put 500000000 into testcase[1]
   put 5 into testcase[2]
   put 999999995 into testcase[3]

   put 10 into runCount

   repeat with i = 1 to runCount
      repeat with testID = 1 to 3
         repeat for each item testType in "sort,repeat,PMB"
            put i && testID && testType into fld 3
            put the long seconds into S
            do format("put greatestLessThan%s(L,testcase[%s]) into
R[%s][%s]",testType,testID,testType,testID)
            add (the long seconds - S) to T[testType][testID]
         end repeat
      end repeat
   end repeat

   put "Run Count:" && runCount & cr & cr into fld 3
   repeat with testID = 1 to 3
      put "Test ID:" && testID && "Looking for greatest value <" &&
testcase[testID] & cr after fld 3
      repeat for each item testType in "sort,repeat,PMB"
         put testType && T[testType][testID]/runCount &&
R[testType][testID] & cr after fld 3
      end repeat
      put cr after fld 3
   end repeat

end mouseUp

function greatestLessThanSort pList,V
   sort items of pList descending numeric
   sort items of pList by each >= V
   return item 1 of pList
end greatestLessThanSort

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

function greatestLessThanPMB tList,maxVal
   repeat for each item i in tList
      if i < maxVal then put i & comma after outList
   end repeat
   if outList is empty then return empty else return max(outList)
end greatestLessThanPMB



More information about the use-livecode mailing list