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

dunbarx at aol.com dunbarx at aol.com
Mon Oct 12 10:24:52 EDT 2015


I made a test between the "sort" method and the "repeat for each". For a list of about 100,000 items, sort finds the max value in 33 ticks, and "repeat" takes 50 ticks.


This seems logical to me, since lower level operations are used in "sort" as opposed to "repeat" even though we all know how fast "repeat for each" is.


Craig 



-----Original Message-----
From: Peter TB Brett <peter.brett at livecode.com>
To: How to use LiveCode <use-livecode at lists.runrev.com>
Sent: Mon, Oct 12, 2015 4:55 am
Subject: Re: function for greatest object in list less than or equal to a value




On 12/10/2015 03:26, Peter M. Brigham 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(item 1 to -1 of
outList)
> end getMaxLessThan

This should be slightly faster, because it
only loops over the items once:

     function getMaxLessThan pList, pLimit
 
local tMaxFound, tItem
        put empty into tMaxFound
        repeat for
each item tItem in tList
           if tItem < pLimit and \
                
(tItem > tMaxFound or tMaxFound is empty) then
              put tItem into
tMaxFound
           end if
        end repeat
        return tMaxFound
    
end getMaxLessThan

The "sort"-based solution will be much less efficient for
long input 
lists because sorting is O(N*log(N)), whereas Peter Brigham's and
my 
solutions are O(N).

                                      Peter

--

Dr Peter Brett <peter.brett at livecode.com>
LiveCode Open Source
Team

LiveCode on reddit:
https://reddit.com/r/livecode

_______________________________________________
use-livecode
mailing list
use-livecode at lists.runrev.com
Please visit this url to subscribe,
unsubscribe and manage your subscription
preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode

 



More information about the use-livecode mailing list