Processor friendly waiting

Walter
Posts: 20
Joined: Mon Oct 06, 2014 1:44 pm
My devices: iPad 3, iPod Touch 5

Processor friendly waiting

Post by Walter »

I have an app where it loops waiting for a button press. If no button is pressed, there is nothing for it to do.

Is there a better way to wait for a button press? It is using up a lot of battery waiting for the button press. I ran the app for around an hour last night, and Battery Usage shows smart BASIC as responsible for 73% of battery drain for the past 24 hours. The next biggest app is 8%.

99.9% of the time the app is just in this loop waiting.

Code: Select all

loop:
if button_pressed("button") then
... do stuff ...
endif
goto loop

User avatar
Mr. Kibernetik
Site Admin
Posts: 4786
Joined: Mon Nov 19, 2012 10:16 pm
My devices: iPhone, iPad, MacBook
Location: Russia
Flag: Russia

Re: Processor friendly waiting

Post by Mr. Kibernetik »

Yes, event driven behavior is a good request. It is in TO DO list.

Walter
Posts: 20
Joined: Mon Oct 06, 2014 1:44 pm
My devices: iPad 3, iPod Touch 5

Re: Processor friendly waiting

Post by Walter »

Event driven would be the best! I'd love to be able to have code connected to a button directly somehow.

If that takes a long time, maybe you could give us a FAST and SLOW command like the UNDERGROUND command that would cause the currently running program to run at full speed, or a slow speed that uses a lot less processor?

Or how about a variation on the GOTO command that would wait a reasonable time before continuing execution. That way the code would run at full speed, but empty loops would slow down?

User avatar
Mr. Kibernetik
Site Admin
Posts: 4786
Joined: Mon Nov 19, 2012 10:16 pm
My devices: iPhone, iPad, MacBook
Location: Russia
Flag: Russia

Re: Processor friendly waiting

Post by Mr. Kibernetik »

Walter wrote:Or how about a variation on the GOTO command that would wait a reasonable time before continuing execution. That way the code would run at full speed, but empty loops would slow down?
You are absolutely right.
It could be some kind of SLOWDOWN command which will delay execution and reduce CPU usage, or a kind of SLOW GOTO for slow looping. I am still thinking about it.

User avatar
Mr. Kibernetik
Site Admin
Posts: 4786
Joined: Mon Nov 19, 2012 10:16 pm
My devices: iPhone, iPad, MacBook
Location: Russia
Flag: Russia

Re: Processor friendly waiting

Post by Mr. Kibernetik »

What delay value you would consider appropriate without looping being too frozen?

Walter
Posts: 20
Joined: Mon Oct 06, 2014 1:44 pm
My devices: iPad 3, iPod Touch 5

Re: Processor friendly waiting

Post by Walter »

I would try a delay of 1/10 - 1/3 of a second. That way the loop would execute several times a second. Just a guess. I'd say try some values and see what seems responsive.

User avatar
Mr. Kibernetik
Site Admin
Posts: 4786
Joined: Mon Nov 19, 2012 10:16 pm
My devices: iPhone, iPad, MacBook
Location: Russia
Flag: Russia

Re: Processor friendly waiting

Post by Mr. Kibernetik »

Walter wrote:I would try a delay of 1/10 - 1/3 of a second. That way the loop would execute several times a second. Just a guess. I'd say try some values and see what seems responsive.
Ok. My experiments show that 0.05 s delay leaves CPU in almost idle state. As it is twice as fast as your suggestion, let it be like that.

User avatar
Mr. Kibernetik
Site Admin
Posts: 4786
Joined: Mon Nov 19, 2012 10:16 pm
My devices: iPhone, iPad, MacBook
Location: Russia
Flag: Russia

Re: Processor friendly waiting

Post by Mr. Kibernetik »

In version 4.8 this feature will be implemented as SLOWDOWN command.

So, code like:

Code: Select all

LOOP: SLOWDOWN
IF NOT BUTTON_PRESS() THEN LOOP
will leave CPU almost idle.

Walter
Posts: 20
Joined: Mon Oct 06, 2014 1:44 pm
My devices: iPad 3, iPod Touch 5

Re: Processor friendly waiting

Post by Walter »

Thanks! Looking forward to it. This should be great to save our batteries until you can get the event driven stuff done.

ericmengyi
Posts: 42
Joined: Thu Mar 12, 2015 7:22 am
My devices: iphone 5s, /ipad Air

Re: Processor friendly waiting

Post by ericmengyi »

What is the difference between Slowdown Command and Pause Command? I mean, could I use "pause 0.05" in the loops to get the same effect?

Post Reply