which is faster for searching?

jbv at souslelogo.com jbv at souslelogo.com
Wed Jun 18 13:35:27 EDT 2014


Regarding code such as :

   repeat for each line j in myVar
      if item 1 of j > 1 and item 2 of j > tMin and item 3 of j < tMax then
      end if
   end repeat

I remember a discussion several years ago on this very list mentioning
that in the "if" line, the engine would drop evaluation as soon as a false
is encountered (which I think is a feature common to many languages now).
Therefore carefully organizing the order of the items in each line of myVar
can also be a way to preserve the speed of "repeat for each" when complex
testing must be done.
I wonder how "filter" compares to this...

Besides, when very complex testing must be done on several items of each
line j,
I found that speed can be even more preserved by spliting each line into
an array and comparing each array cell to a refernce array...
Example :
   put 1 into Tref1["a"]
   put 1 into Tref1["b"]
   put 1 into Tref2[4]
   put 1 into Tref2[15]
   put 1 into Tref2[26]

   repeat for each line j in myVar
      split j using tab
      if Tref1[j[1]] is not empty and Tref2[j[2]] is not empty then
      end if
   end repeat

jbv


> "Filter" is a wonderful command that's almost always much faster than
> "repeat for each...", provided you're looking for data across a
> collection comprised of single-column rows.
>
> Once you get to multiple columns and you're searching just one of them,
> "repeat for each..." scales linearly quite nicely but the performance of
> "filter" degrades almost geometrically.
>





More information about the use-livecode mailing list