Author Topic: how to time something (ie do loop for n seconds)  (Read 4520 times)

0 Members and 1 Guest are viewing this topic.

Offline madscijr

  • Seasoned Forum Regular
  • Posts: 295
    • View Profile
how to time something (ie do loop for n seconds)
« on: February 23, 2022, 11:01:09 am »
Thanks to @SMcNeill who taught me a little something:
"How do you time QB64 to do something for a certain amount of time?"

I thought maybe an example would come in handy for other QB64 newbies or users who haven't seen this.
(It certainly came in handy for me.)

Code: QB64: [Select]
  1.     ' DO SOMETHING FOR 3 SECONDS
  2.     t# = Timer + 3
  3.     Do
  4.         '(SOMETHING)
  5.     Loop Until Timer > t#


Original thread this came from, where Steve explains hardware images, which speed up graphics in QB64 big time:
https://qb64forum.alephc.xyz/index.php?topic=4674.0

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: how to time something (ie do loop for n seconds)
« Reply #1 on: February 23, 2022, 11:26:52 am »
One caveat here:  You *can* experience bugs with this after midnight.

Program starts at 23:59:59.  Add three seconds -- 24:00:02...  (In seconds, and not hours and minutes like this, though hours and minutes are easier to visualize.)

Clock hits midnight:  0:00:00

At no point will you ever have TIMER become greater than t#.

If you're going to have a program which might run into this issue, I'd suggest just plugging in my ExtendedTimer and use it instead:

Code: QB64: [Select]
  1. FUNCTION ExtendedTimer##
  2.     d$ = DATE$
  3.     l = INSTR(d$, "-")
  4.     l1 = INSTR(l + 1, d$, "-")
  5.     m = VAL(LEFT$(d$, l))
  6.     d = VAL(MID$(d$, l + 1))
  7.     y = VAL(MID$(d$, l1 + 1)) - 1970
  8.     FOR i = 1 TO m
  9.         SELECT CASE i 'Add the number of days for each previous month passed
  10.             CASE 1: d = d 'January doestn't have any carry over days.
  11.             CASE 2, 4, 6, 8, 9, 11: d = d + 31
  12.             CASE 3: d = d + 28
  13.             CASE 5, 7, 10, 12: d = d + 30
  14.         END SELECT
  15.     NEXT
  16.     FOR i = 1 TO y
  17.         d = d + 365
  18.     NEXT
  19.     FOR i = 2 TO y STEP 4
  20.         IF m > 2 THEN d = d + 1 'add an extra day for leap year every 4 years, starting in 1970
  21.     NEXT
  22.     d = d - 1 'for year 2000
  23.     s~&& = d * 24 * 60 * 60 'Seconds are days * 24 hours * 60 minutes * 60 seconds
  24.     ExtendedTimer## = (s~&& + TIMER)

Most of us write time code to test little snippets for which method might be faster for us while we're coding.  The clock resetting on us isn't normally such a big deal.  When it is, however, all you have to do is swap to the ExtendedTimer function above -- it returns a value for you based off DAY + TIME, rather than just time alone!  No midnight clock issues with something like that in our programs.  ;)

    ' DO SOMETHING FOR 3 SECONDS
    t# = ExtendedTimer + 3
    Do
        '(SOMETHING)
    Loop Until Timer > t#
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline madscijr

  • Seasoned Forum Regular
  • Posts: 295
    • View Profile
Re: how to time something (ie do loop for n seconds)
« Reply #2 on: February 23, 2022, 12:02:34 pm »
One caveat here:  You *can* experience bugs with this after midnight.
...
I'd suggest just plugging in my ExtendedTimer and use it instead:
...

That could definitely throw a wrench in the works, especially for us night owls or for any programs running around midnight. I'll add your routine to the toolbox. And thanks!

Offline mdijkens

  • Newbie
  • Posts: 34
    • View Profile
Re: how to time something (ie do loop for n seconds)
« Reply #3 on: February 28, 2022, 05:35:39 pm »
One caveat here:  You *can* experience bugs with this after midnight.

Program starts at 23:59:59.  Add three seconds -- 24:00:02...  (In seconds, and not hours and minutes like this, though hours and minutes are easier to visualize.)

Clock hits midnight:  0:00:00

At no point will you ever have TIMER become greater than t#.

If it is not that critical for normal situations I always used:

Code: QB64: [Select]
  1. Tim! = Timer
  2. Do While Abs(Timer - Tim!) < 10
  3.   ' Try something over and over again for 10 seconds
  4.  

and yes, around midnight you might have one shorter loop, but in most cases that's not so critical
~ 2 million lines of BASIC

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: how to time something (ie do loop for n seconds)
« Reply #4 on: February 28, 2022, 05:55:18 pm »
add a check for passing midnight
If Timer - Tim! < 0 then add days worth of seconds = 24 * 60 *60 else daysworth = 0

then do the
If timer + daysworth - Tim! ....

Offline mdijkens

  • Newbie
  • Posts: 34
    • View Profile
Re: how to time something (ie do loop for n seconds)
« Reply #5 on: February 28, 2022, 06:15:25 pm »
If it is important without an extra condition
Code: QB64: [Select]
  1. Tim! = Timer
  2. Do While (86400 + Timer - Tim!) Mod 86400 < 10
  3.   ' Try something over and over again for 10 seconds
  4.  
~ 2 million lines of BASIC

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: how to time something (ie do loop for n seconds)
« Reply #6 on: February 28, 2022, 06:27:44 pm »
Nice that might work!

Offline CharlieJV

  • Newbie
  • Posts: 89
    • View Profile
Re: how to time something (ie do loop for n seconds)
« Reply #7 on: February 28, 2022, 08:02:06 pm »
One caveat here:  You *can* experience bugs with this after midnight.

(SNIP!)

Well, reflexes of a sloth here, yet this was instantaneous: