App running too slowly. Advice needed plz...

Getting into LiveCode for iOS? Ask your questions here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
paulsr
Posts: 222
Joined: Thu Jul 19, 2012 9:47 am
Contact:

App running too slowly. Advice needed plz...

Post by paulsr » Thu Oct 18, 2012 3:47 am

Greetings:

This is difficult to explain, but I have an app that is running too slowly. Being fairly new to Livecode I suspect I've chosen the wrong way to code it, and would appreciate some advice on the best way to re-code.

At the moment (by which I mean I intend to add further functions) the app displays a digital clock, showing hh:mm:ss, and two stop watches, which display to tenths of seconds. The stop watches are started/paused by tapping the displays, and reset via a swipe. The clock is, well, a clock, so it just runs.

Until yesterday, I had been testing on the iPad simulator. With both stop watches running along with clock, the display was kind-of jerky... with not all of the tenths of seconds showing. I figured, it's just a simulator, so yesterday I transferred the app to my iPad1.

On the iPad the app is a disaster. With all three displays running, the iPad seems to be seriously overloaded. Tapping a display to pause a stop watch can take five seconds before the app responds. And the displays only get updated every one to two seconds. It's useless.

A while ago, before knowing anything about Livecode, I developed this with a little html and several kilometers of javascript. Running in a browser on the iPad, it's fine. Fully functional, no delays.

So, clearly I have done something stupid in Livecode!

I have three routines, one for the clock and one each for the stopwatches. RunCLK, RunSW1 and RunSW2.

At the end of the openCard script, I simply call these three routines to start them.

At the end of RunCLK I have...

Code: Select all

send "RunCLK" to me in 250 milliseconds
and at the end of the stop watch scripts I have ...

Code: Select all

send "RunSW1" to me in 50 milliseconds

Code: Select all

send "RunSW2" to me in 50 milliseconds
The clock only displays to the nearest second, so 250ms is fine, whereas with the stop watches I want to have a smooth display of the tenths.

So; what did I do wrong ???

I'm guessing it's the "send blahblahblah to me in ..." that's causing the problems. Should I have coded this in one routine that calls itself every 50ms? Would that help?

Or is there some totally different way to achieve this?

Any thoughts/advice would be greatly appreciated coz right now I have good looking app that is totally useless.

Many thanks...

--paul

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7389
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: App running too slowly. Advice needed plz...

Post by jacque » Thu Oct 18, 2012 11:31 pm

You're doing it the right way, but the timing may be causing the messages to get backlogged. Try checking the message queue before sending a message again:

if "RunCLK" is not in the pendingmessages then send "RunCLK" to me in 250 milliseconds

Do that for each one. If that doesn't help, let us know and we'll guess some more.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

paulsr
Posts: 222
Joined: Thu Jul 19, 2012 9:47 am
Contact:

Re: App running too slowly. Advice needed plz...

Post by paulsr » Fri Oct 19, 2012 8:03 am

Thank you Jacqueline .. I tried that, but it didn't make any noticeable difference.

I'm thinking maybe everything should be in one routine ... combining the code of RunCLK, RunSW1 & RunSW2. Do you think that would help?

The performance is at its worst when the time-of-day clock and both of the stop watches are running, so it seems its the "send --- to me in ---" that slows everything down.

Thanks for any further thoughts...

--paul

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7389
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: App running too slowly. Advice needed plz...

Post by jacque » Fri Oct 19, 2012 8:18 pm

I've never seen a slowdown due to the "send" command except for those I introduced myself, but yeah, it's possible something in your scripts is causing a delay. But the delay may not be the scripts, it may be due to the engine redraw speed. If you haven't done it yet, set the layermode of all the moving objects to "dynamic" (you can do that in each object's property inspector.) That provides a big increase in speed for objects that move continuously. Your script should also set the acceleratedRendering of the stack to true (see the dictionary) so that the layermode will be used by the engine.

I'm betting this will solve a lot of things.

Correction: the layermode should be set for the groups, not the objects inside the group. Always set the layermode to the highest-level object; the owner of the object must be the card itself for the layermode to work.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

paulsr
Posts: 222
Joined: Thu Jul 19, 2012 9:47 am
Contact:

Re: App running too slowly. Advice needed plz...

Post by paulsr » Sat Oct 20, 2012 4:43 am

Thank you again Jacqueline. I was hoping your answer would be in English, but I'll try to translate it later today. :D

It's seems there's a huge amount of Livecode I still have to learn!

--paul

paulsr
Posts: 222
Joined: Thu Jul 19, 2012 9:47 am
Contact:

Re: App running too slowly. Advice needed plz...

Post by paulsr » Sat Oct 20, 2012 7:38 am

jacque wrote:Correction: the layermode should be set for the groups, not the objects inside the group. Always set the layermode to the highest-level object; the owner of the object must be the card itself for the layermode to work.
Hmm, let me tell you what I've done, in case it's wrong...

Each of the three displays has its own group. Each of the digits of stop watch 1, for instance, is in group "GprSW1". So I have set the three groups layer modes to "Dynamic." I have not changed anything within those groups.

I have also put

Code: Select all

set the acceleratedRendering of this stack to true
into the preOpenStack handler. Is this the correct place?

I don't understand your comment "the owner of the object must be the card itself" so maybe there's something else I need to do?

Anyhoo; sorry to say, performance is now worse! The clock runs okay, because it has no tenths of seconds. If I start one of the stop watches, it only updates its display every 0.8secs. If I start both stop watches, the displays only update every 1.6secs. Any on-screen tap takes about 4 seconds to be actioned.

I'm thinking I have not done quite what you were suggesting. Plz could you advise...

Thanks

--paul

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7389
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: App running too slowly. Advice needed plz...

Post by jacque » Sat Oct 20, 2012 9:10 pm

That's too bad, I was hoping it would work. It looks like you did it right. AcceleratedRendering is mostly for objects that move quickly (user dragging, for example, or objects repositioned with the "move" command) but I was hoping it would work for this too. Seems like it should. The down side of acceleratedRendering is that it will slow down performance if there are no moving parts on the card.

What I mean by the owner being the card was just that the object you set to dynamic needs to be on the card itself, not a member of group.

Typically acceleratedRendering is set in preOpenCard, you might try that.

If that doesn't work, can you post your scripts? Or a link to a copy of the stack so we can look?
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

Cool Dave
Posts: 35
Joined: Mon Jun 25, 2012 8:44 pm

Re: App running too slowly. Advice needed plz...

Post by Cool Dave » Sat Oct 20, 2012 9:34 pm

I once made a Windows app that was doing much the same thing. I had a number that was constanly climbing, sort of like a national debt clock. But it was acting really slow and choppy, and even caused livecode to freeze up.

Turned out the problem was the display. I had seven fields, one for each digit, and updating them was too much. I downloaded a different font (Digits DB), which like courier has equal-width characters, and used just one field. The result was incredible...it was many times faster.

Hope this helps!
Dave

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

Re: App running too slowly. Advice needed plz...

Post by sturgis » Sat Oct 20, 2012 9:41 pm

You might try a couple things. First limit it to a single send loop. Something like the following:

Code: Select all

command watchLoop
   lock screen -- lock the screen for speed purposes. Only 1 screen update this way. 
   updateClock --could set a timer on this an only update it every so often but probably not necessary
   if sw1 then stopwatch1 -- a flag. If sw1 is empty or false (declared as a script local variable) do nothing otherwise update teh sw
   if sw2 then stopwatch2   -- same for sw2
   send "watchLoop" to me in 35 milliseconds -- adjust the delay as you see fit to get the results you want.  
   unlock screen
end watchLoop
A single loop should be more efficient than 3 loops. There is a little overhead and delay with send (think dispatch is faster but can't delay with dispatch) You're already looping very fast (the 50 millisec send loop) so you might as well use it for both stop watches anyway and use flags to determine behavior for each loop. This i how game loops work too, have one main loop to update the screen and that loop runs whatever handlers are needed based on circumstances as they change.

The second thing I was going to point out is the lock screen, but its in the code and commented so there ya go.

Of course my handler names are just examples and its possible there is more going on with your code that could be optimized. Hard to say without seeing actual code.

paulsr
Posts: 222
Joined: Thu Jul 19, 2012 9:47 am
Contact:

Re: App running too slowly. Advice needed plz...

Post by paulsr » Sun Oct 21, 2012 10:57 am

Cool Dave wrote:I once made a Windows app that was doing much the same thing. I had a number that was constanly climbing, sort of like a national debt clock. But it was acting really slow and choppy, and even caused livecode to freeze up.

Turned out the problem was the display. I had seven fields, one for each digit, and updating them was too much. I downloaded a different font (Digits DB), which like courier has equal-width characters, and used just one field. The result was incredible...it was many times faster.

Hope this helps!
Dave
Thanks Dave. I thought that might help, but unfortunately it didn't.

All I did was comment out the update of all but the last two digits of the clock & stop watches, and the delay seemed to be the same, so I guess we can rule that out as the cause.

--paul

paulsr
Posts: 222
Joined: Thu Jul 19, 2012 9:47 am
Contact:

Re: App running too slowly. Advice needed plz...

Post by paulsr » Sun Oct 21, 2012 11:11 am

jacque wrote:That's too bad, I was hoping it would work. It looks like you did it right. AcceleratedRendering is mostly for objects that move quickly (user dragging, for example, or objects repositioned with the "move" command) but I was hoping it would work for this too. Seems like it should. The down side of acceleratedRendering is that it will slow down performance if there are no moving parts on the card.

What I mean by the owner being the card was just that the object you set to dynamic needs to be on the card itself, not a member of group.

Typically acceleratedRendering is set in preOpenCard, you might try that.

If that doesn't work, can you post your scripts? Or a link to a copy of the stack so we can look?
Okay, first off, I moved the accelleratedRendering as you suggested, but no change.

Here is the code for the clock update. The stop watch updates are very similar, except they have to react to taps'n'swipes.

Code: Select all

command RunClock
   global the_clock_message_id
   put the long time into tTime
   convert tTime to seconds
   add gTimeAdj to tTime
   convert tTime to long time
   replace ":" with " " in tTime
   
   -- handle date changes
   if tTime = "00 00 00" then
      put the system time into tDate
      convert tDate to seconds
      add gTimeAdj to tDate
      convert tDate to abbrev date
      replace "," with "" in tDate
      put word 1 of tDate & return & return & word 2 of tDate & return & return & word 3 of tDate into fld "LblCLKd9"
   end if
   
   put character 1 of word 1 of tTime into fld "LblCLKd5"
   put character 2 of word 1 of tTime into fld "LblCLKd4"
   put character 1 of word 2 of tTime into fld "LblCLKd3"
   put character 2 of word 2 of tTime into fld "LblCLKd2"
   put character 1 of word 3 of tTime into fld "LblCLKd1"
   put character 2 of word 3 of tTime into fld "LblCLKd0"
   
   if "RunClock" is not in the pendingmessages then send "RunClock" to me in 250 milliseconds
   put the result into the_clock_message_id
end RunClock
At the end of the openCard there is a simple "RunClock".

I think that whatever error I've made is common to the clock and stopwatch scripts because I've just tested it on the iPad with the stopwatch parts commented out. I watched the clock updates carefully, and can see that the updates of the last digit of the seconds is very lumpy. Suddenly there will be a pause, and then only a fraction of a second between updates.

Something else that might be relevant ... when I test this in Livecode on my Windoze PC, running only the clock ... it will run for a random amount of time (anywhere between five seconds and five minutes) and then, without warning, will completely crash. By which I mean Livecode just disappears. This doesn't happen on my Mac or iPad, but it must indicate there is some bad coding in there somewhere.

Can you see what I've done wrong? Many tkx...

--paul

PS Looking at the code now, I realize I don't really understand when I should use functions and when I should use commands. Should this be a function? Does it make any difference?

paulsr
Posts: 222
Joined: Thu Jul 19, 2012 9:47 am
Contact:

Re: App running too slowly. Advice needed plz...

Post by paulsr » Sun Oct 21, 2012 11:34 am

sturgis wrote:The second thing I was going to point out is the lock screen, but its in the code and commented so there ya go.
That's it! The lock screen fixed the problem ... well almost.

Now, for some reason one of the stop watches does not respond to taps, but I'm guessing that's just pointing me to a different error.

But with the clock and one stop watch running, the updates are super-smooth. Just the way I expected them to be.

Thanks Sturgis for the solution, and to everyone for the advice.

--paul

Post Reply