Slow Performance Setting the backColor property of Text

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
tjm167us
Posts: 50
Joined: Sat Dec 03, 2011 8:27 pm

Slow Performance Setting the backColor property of Text

Post by tjm167us » Sat Oct 19, 2013 3:44 pm

Hi everyone,

First, the questions I hope to have answered:
Why does setting the backColor property take so long?
What can be done speed up the performance?

The background:
I developed a searchable text field that highlights all instances of whatever characters you type into the search box. It works great, BUT, for search entries where there are a lot of occurrences, the performance is painstakingly slow. The process for the "search" can be broken down into two parts:

1. Find the location of all occurrences of the search entry (and store in an array)
2. Highlight all occurrences of the search entry

I assumed it was the first item that wasn't very efficient, but after some timing tests, I realized it was the highlighting process that was slow. Below are some results from the timing tests:

Finding all r's took: 0.001seconds
Highlighting all r's took: 1.877seconds
Number of Characters: 166

Finding all p's took: 0.001seconds
Highlighting all p's took: 0.242seconds
Number of Characters: 36

Finding all l's took: 0.001seconds
Highlighting all l's took: 0.665seconds
Number of Characters: 65

Finding all a's took: 0.002seconds
Highlighting all a's took: 3.35seconds
Number of Characters: 210

The highlighting code that is being used is:

Code: Select all

on doInitialHighlite 

   --do the highlighting here...
   set the backColor of char 1 to (number of chars in field "searchField") of field "searchField" to empty
   set the backColor of field "searchField" to "#CCCCCC"
   set the locktext of field "searchField" to true
   
   get the keys of theResultsArray
   repeat for each line tLine in it
      put theResultsArray[tLine]["First Char"] into theFirstChar
      put theResultsArray[tLine]["Last Char"] into theLastChar
      if tLine = 1 then
         put 1 into theIndex
         set the hiliteColor of field "searchField" to "yellow"
         --         select the char theFirstChar to theLastChar of field "searchField"       
         set the backColor of char theFirstChar to theLastChar of field "searchField" to "yellow"
      else
         set the backColor of char theFirstChar to theLastChar of field "searchField" to "#FFFFFF"
      end if
   end repeat 

end doInitialHighlite
Where this gets really interesting is when the timing tests were done within this routine. The thing that takes multiple seconds has been tracked to the following line of code:

Code: Select all

set the backColor of char theFirstChar to theLastChar of field "searchField" to "#FFFFFF"
I have an idea for fixing this, but it is not pretty.
Cheers,
Tom

Klaus
Posts: 14208
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Slow Performance Setting the backColor property of Text

Post by Klaus » Sat Oct 19, 2013 3:49 pm

Hi Tom,

UN-/LOCK SCREEN should speed up things:

Code: Select all

command doInitialHighlite
   lock screen
   ## all other stuff here...
   ### ...
   unlock screen
end doInitialHighlite
Best

Klaus

tjm167us
Posts: 50
Joined: Sat Dec 03, 2011 8:27 pm

Re: Slow Performance Setting the backColor property of Text

Post by tjm167us » Sat Oct 19, 2013 4:32 pm

Klaus, Thank you! It's actually really funny, I just finished using lockScreen doing a completely unrelated task, but didn't think of using it to improve the performance of setting the backColor property (duh!). Below are the results of a couple of timing tests with the change:

Finding all a's took: 0.001seconds
Highlighting all a's took: 0.009seconds
Number of Characters: 210

Finding all e's took: 0.002seconds
Highlighting all e's took: 0.013seconds
Number of Characters: 221

Finding all t's took: 0.002seconds
Highlighting all t's took: 0.009seconds
Number of Characters: 195

Finding all p's took: 0.001seconds
Highlighting all p's took: 0.002seconds
Number of Characters: 36

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10354
Joined: Wed May 06, 2009 2:28 pm

Re: Slow Performance Setting the backColor property of Text

Post by dunbarx » Sat Oct 19, 2013 11:23 pm

What Klaus said.

What you might glean from this is that your repeat loop contains a lot of lines where the screen is redrawn. Changing the color of a chunk of field data requires such an action. If you need to see these develop, then fine. Of course you do not, in this case, since only the final state of affairs is pertinent. And that saves a whole bunch of time. Screen redraws are MUCH slower than the commands that implement them.

Craig Newman

Post Reply