faster performance?

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
mtecedor
Posts: 68
Joined: Fri Sep 04, 2009 6:40 pm

faster performance?

Post by mtecedor » Sat May 29, 2010 5:26 pm

Hi again,

I would like to know if there is any trick to make the transition from one card to another faster.

The thing is that since I added some code on OpenCard, it takes few second more to open this card.

The code involves going over the chars of a field text in the previous card, copying some of it and making some changes.

Is there any other place where I can put that piece of code, or any line I can add to the code that makes it faster?

Thanks a lot,

Marta

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

Re: faster performance?

Post by Klaus » Sat May 29, 2010 5:29 pm

Hi Marta,

could you post (some of) the script(s) involved please?


Best

Klaus

mtecedor
Posts: 68
Joined: Fri Sep 04, 2009 6:40 pm

Re: faster performance?

Post by mtecedor » Sat May 29, 2010 5:44 pm

Sure!

Here it is:

Code: Select all

on preOpenCard
  put empty into field "output" of card "Paraphrasing" of stack "Estratego"
  repeat with i = 1 to the number of chars of field "Text2" of card "Text" of stack "Estratego"
     put the backgroundcolor of char i of field "Text2" of card "Text" of stack "Estratego" into tColor
     if tColor is "255,255,0" or tColor is "0,255,0" then
        if tColor is "255,255,0" then  -- this is yellow
           put char i of field "text2" of card "Text" of stack "Estratego" after field "output" of card "Paraphrasing" of stack "Estratego"
           set the textStyle of last char of field "output" of card "Paraphrasing" of stack "Estratego" to normal
        end if
        if tColor is "0,255,0" then -- this is green
           put char i of field "text2" of card "Text" of stack "Estratego" after field "output" of card "Paraphrasing" of stack "Estratego"
           set the textStyle of last char of field "output" of card "Paraphrasing" of stack "Estratego" to bold
        end if
     else
        put return after field "output" of card "Paraphrasing" of stack "Estratego"
     end if
      if tColor is "253, 99, 159" then -- this is pink
           put char i of field "Text2" of card "Text" of stack "Estratego" after field "Vocab" of card "Vocab" of stack "Vocab"
           set the textStyle of last char of field "Vocab" of card "Vocab" of stack "Vocab" to normal
        end if
  end repeat
  repeat with i = the number of lines of field "output" of card "Paraphrasing" of stack "Estratego" down to 1
     if line i of field "output" of card "Paraphrasing" of stack "Estratego" = "" then delete line i of field "output" of card "Paraphrasing" of stack "Estratego"
end repeat

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

Re: faster performance?

Post by Klaus » Sun May 30, 2010 1:32 pm

Hi Marta,

OK, try this, maybe even "on opencard" since "lock screen" does speed up tings a lot.

Code: Select all

on preOpenCard

  ## Always use a "lock screen" when manipulatinmg interface elements, especially content of fields!
  lock screen
  put empty into field "output" of card "Paraphrasing" of stack "Estratego"
  repeat with i = 1 to the number of chars of field "Text2" of card "Text" of stack "Estratego"
    put the backgroundcolor of char i of field "Text2" of card "Text" of stack "Estratego" into tColor
    
    ## I always prefer "switcvh" isntead of many nested "if...then..." conditions.
    switch tColor
    case "255,255,0"  -- this is yellow
      put char i of field "text2" of card "Text" of stack "Estratego" after field "output" of card "Paraphrasing" of stack "Estratego"
      set the textStyle of last char of field "output" of card "Paraphrasing" of stack "Estratego" to normal
      break
    case "0,255,0"  -- this is green
      put char i of field "text2" of card "Text" of stack "Estratego" after field "output" of card "Paraphrasing" of stack "Estratego"
      set the textStyle of last char of field "output" of card "Paraphrasing" of stack "Estratego" to bold
      break
    default
     ## Neither YELLOW nor GREEN
      put return after field "output" of card "Paraphrasing" of stack "Estratego"
      break
    end switch
    
    if tColor = "253, 99, 159" then -- this is pink
      put char i of field "Text2" of card "Text" of stack "Estratego" after field "Vocab" of card "Vocab" of stack "Vocab"
      set the textStyle of last char of field "Vocab" of card "Vocab" of stack "Vocab" to normal
    end if
  end repeat
  ## In this case we can get the coplete content of the field, filter out all empty lines! and write it back into the field
  ## this is faster than directly manipulating the field line by line!
  put field "output" of card "Paraphrasing" of stack "Estratego" into tOutput
  filter tOutput without empty
  put tOutput into field "output" of card "Paraphrasing" of stack "Estratego"
  unlock screen
end preopencard
Please check if this will be faster than before.
And as I mentioned above, you could also try this "on opencard".


Best

Klaus

mtecedor
Posts: 68
Joined: Fri Sep 04, 2009 6:40 pm

Re: faster performance?

Post by mtecedor » Sun May 30, 2010 2:42 pm

Klaus,

Putting the code on OpenCard definetely makes a difference. Thanks a lot. And your code looks much more simpler and elegant than mine, but what I don't understand is why with your code words highlighted in green are not bolded. The line of code is there, but it does not work.

Code: Select all

case "0,255,0"  -- this is green
      put char i of field "text2" of card "Text" of stack "Estratego" after field "output" of card "Paraphrasing" of stack "Estratego"
      set the textStyle of last char of field "output" of card "Paraphrasing" of stack "Estratego" to bold
      break
I am not that familiar with switch expressions (I think I used it once for something really simple), so I dont understand why is not doing it

Thanks

Marta

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

Re: faster performance?

Post by Klaus » Sun May 30, 2010 3:02 pm

Hi Marta,

glad it speeds up your performance!
The performance of you scripts, of course :)

Sorry, no idea why the last char will not get bold?
This is definitvely not a problem of the "switch" structure, since the char gets put after field "output", right?

Hint:
Since modifying contents of fields is quite time consuming, maybe you could figure out another method of working with the field?
Maybe manipulating the HTML text and then setting it back to your field? This is much faster in any case!
I mean, how do single chars get a different hilite in your app design?
Sorry, I just have no idea how your app might work :)


Best

Klaus

Post Reply