Speed up a slow loop

J. Landman Gay jacque at hyperactivesw.com
Sun Mar 6 15:07:43 EST 2022


On 3/6/22 7:33 AM, Alex Tweedly via use-livecode wrote:
> Could you maybe post the code that you're using that takes 5ms ?

It does a bit more than your test, and I only estimated the number of user words since I'd 
tried so many things. I just tested again and this list has 173 words. The dictionary array has 
been reduced as per Quentin's suggestion.

I need to get two lists, one for correct words and another for incorrect words. I used to hit 
the array directly but my current method uses the difference command. Here's the lookup I'm 
using, sDictFile is a script local array:

function checkDictionary pList -- check dictionary for valid words
   put empty into tNonWords
   split pList by cr and tab
   difference pList with sDictFile into tNonWords

   set wholematches to true
   put fld "wordList" into tWList -- no longer the same as pList
   lock screen
   repeat for each key k in tNonWords -- mark non-words in list
     set the textcolor of word 1 of line lineoffset(k,tWList) of fld "wordList" to "red"
   end repeat
   unlock screen
   if tNonWords <> "" then put keys(tNonWords) into sStats["unknowns"] -- stats for reporting later

   difference pList with tNonWords
   return keys(pList)
end checkDictionary

I thought the extra time might be due to the field updates, but when I added 8 incorrect words 
the time didn't change. It remains steady at 5ms.

So I switched back to hitting the dictionary array directly, and the time for 173 valid words 
and 8 invalid words dropped (on Mac) to 1. :)

function checkDictionary pList -- check dictionary for valid words
   set wholematches to true
   repeat for each line l in pList
     if sDictFile[l] = true then
       put l & cr after tValidWords
     else
       put l & cr after tNonWords
     end if
   end repeat
   lock screen
   repeat for each line l in tNonWords -- mark non-words in list
     set the textcolor of word 1 of line lineoffset(l,pList) of fld "wordList" to "red"
   end repeat
   unlock screen
   if tNonWords <> "" then put tNonWords into sStats["unknowns"]
   return tValidWords
end checkDictionary

So there you have it. I'll go back to the original method. I'm glad you questioned this.


-- 
Jacqueline Landman Gay         |     jacque at hyperactivesw.com
HyperActive Software           |     http://www.hyperactivesw.com



More information about the use-livecode mailing list