Author Topic: TIMER vs SLEEP  (Read 1794 times)

0 Members and 1 Guest are viewing this topic.

Offline STxAxTIC

  • Library Staff
  • Forum Resident
  • Posts: 1091
  • he lives
TIMER vs SLEEP
« on: March 28, 2022, 07:57:16 am »
What happens when the unstoppable force of TIMER meets the immovable object of SLEEP?

Code: QB64: [Select]
  1. Screen _NewImage(600, 600, 32)
  2.  
  3. On Timer(t1, .01) moo
  4. Timer(t1) On
  5.  
  6.     PSet (Rnd * _Width, Rnd * _Height), _RGB(255, 255, 255)
  7.     'Sleep
  8.     _Display
  9.  
  10.  
  11. Sub moo
  12.     'do nothing

When there's a Timer at play, SLEEP never fully halts the program - or, it sorta does. It slows the thing down in kindof an interesting way. Not that anybody should ever depend on SLEEP for anything, so it's weird that this question would come up in the first place.... anyway... Thought I'd share.
You're not done when it works, you're done when it's right.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
Re: TIMER vs SLEEP
« Reply #1 on: March 28, 2022, 11:04:50 am »
Code: QB64: [Select]
  1. On Timer(t1, .01) moo
  ' set at every 100th of a sec doesn't give much time for sleeping.

How many dots can you make in 2 secs? = how many times you press a key in 2 secs.
Code: QB64: [Select]
  1. Screen _NewImage(600, 600, 32)
  2.  
  3. On Timer(t1, 2) moo  ' <<< every 2 secs
  4. Timer(t1) On
  5.  
  6.     PSet (Rnd * _Width, Rnd * _Height), _RGB(255, 255, 255)
  7.     Sleep
  8.  
  9.  
  10. Sub moo
  11.     Cls
  12.  

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
Re: TIMER vs SLEEP
« Reply #2 on: March 28, 2022, 02:54:21 pm »
I'd recommend DO: SLEEP: LOOP, but I'd never hear the END of it.

Pete
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
Re: TIMER vs SLEEP
« Reply #3 on: March 28, 2022, 02:56:22 pm »
Ha, that's what I do and I don't want to hear the end of it.

Offline micronoise

  • Newbie
  • Posts: 6
Re: TIMER vs SLEEP
« Reply #4 on: March 30, 2022, 07:04:22 pm »
I'd recommend DO: SLEEP: LOOP, but I'd never hear the END of it.

Pete

DO:SLEEP:A$=INKEY$:LOOP UNTIL whatever you need A$ to be

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • Steve’s QB64 Archive Forum
Re: TIMER vs SLEEP
« Reply #5 on: March 30, 2022, 09:59:07 pm »
DO:SLEEP:A$=INKEY$:LOOP UNTIL whatever you need A$ to be

DO
   a$ = INPUT$(1)
LOOP UNTIL a$ = whatever$

Why the need for SLEEP in there?
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
Re: TIMER vs SLEEP
« Reply #6 on: March 30, 2022, 10:08:45 pm »
Pete's screwing around and micronoise took him serious.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • Steve’s QB64 Archive Forum
Re: TIMER vs SLEEP
« Reply #7 on: March 30, 2022, 10:17:55 pm »
Pete's screwing around and micronoise took him serious.

Pete *NEVER* screws around -- just ask his wife!  Celibate for 50 years now...

It's no wonder she likes me better.  😘
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline micronoise

  • Newbie
  • Posts: 6
Re: TIMER vs SLEEP
« Reply #8 on: March 31, 2022, 06:29:44 am »
Why the need for SLEEP in there?

Reduce CPU usage.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
Re: TIMER vs SLEEP
« Reply #9 on: March 31, 2022, 06:33:31 am »
@micronoise Why do the Do if all you do is sleep? There's the waste!

I'd do it this way:
Code: QB64: [Select]
  1.     'do something
  2.     i = i + 1
  3.     Print i
  4.     Sleep
  5.  

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • Steve’s QB64 Archive Forum
Re: TIMER vs SLEEP
« Reply #10 on: March 31, 2022, 07:17:58 am »
Reduce CPU usage.

You do realize that SLEEP isn't the only command which limits CPU usage, right?

DO
   a$ = INPUT$(1) <--  this drops CPU usage to almost nothing as well, while waiting for a single key press


Or, just as simple:

DO
   a$ = INKEY$
   _LIMIT 30
LOOP UNTIL a$ = whatever$



SLEEP is honestly a problematic way to go about this type of process, as it's basically nothing more than a DO: _DELAY 0.01: LOOP UNTIL _KEYHIT type process, and there's some old versions of QB64 where SLEEP breaks with unwanted out-of-focus keyhits.  (Such as SHIFT-TAB which should be OS reserved to switch app focus between programs.)

INPUT$(1) works much better for a hard pause, while _LIMIT or _DELAY is better with INKEY$ or _KEYHIT, if you make use of ON TIMER events.

(At least, in my opinion...)
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
Re: TIMER vs SLEEP
« Reply #11 on: March 31, 2022, 07:22:35 am »
Sleep let's you look at whatever you've done in 1 loop for however long you like, it's a real advantage for debugging as long as you don't have something on a timer.

Offline micronoise

  • Newbie
  • Posts: 6
Re: TIMER vs SLEEP
« Reply #12 on: March 31, 2022, 10:15:46 am »
Maybe a matter of habit ;-) Bear with me, I still consider my self as a newbie. a$=input$(1) is probably better.

Offline Cobalt

  • QB64 Developer
  • Forum Resident
  • Posts: 878
  • At 60 I become highly radioactive!
Re: TIMER vs SLEEP
« Reply #13 on: April 01, 2022, 12:13:20 am »
There is also this nice little feature:

TIMER(BLAH) OFF

that you can call before SLEEPing

then

TIMER(BLAH) ON

when you wake up
Granted after becoming radioactive I only have a half-life!

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
Re: TIMER vs SLEEP
« Reply #14 on: April 02, 2022, 06:07:33 pm »
Hey Steve, don't make me go Will Smith on you! I'd have my wife handle it, but she was out late last night. Ouch, what's this hay doing in the bed? :D

Pete
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/