Author Topic: Use TIME$ to calculate elapsed time to tenth of a second?  (Read 3010 times)

0 Members and 1 Guest are viewing this topic.

Offline Statsman1

  • Newbie
  • Posts: 36
  • I'm just a jerk, but a hero is what I want to be.
    • View Profile
Use TIME$ to calculate elapsed time to tenth of a second?
« on: September 23, 2021, 08:02:44 pm »
Hey guys...

I am trying to capture the elapsed time of on-screen event, using Time$, and I can definitely get the seconds, but is there a way to calculate elapsed tenths of a second?

I am using val(right$(time$,2)) to get the seconds, but I am finding that capturing the tenths of a second is not evident.

Thank you!
Good decisions come from experience.
Experience comes from bad decisions.

FellippeHeitor

  • Guest
Re: Use TIME$ to calculate elapsed time to tenth of a second?
« Reply #1 on: September 23, 2021, 08:19:58 pm »
TIMER returns how many seconds have elapsed since midnight.

Code: QB64: [Select]
  1. start! = Timer
  2. finish! = Timer
  3. PRINT "Time elapsed:"; finish! - start!

FellippeHeitor

  • Guest
Re: Use TIME$ to calculate elapsed time to tenth of a second?
« Reply #2 on: September 23, 2021, 08:21:09 pm »
Welcome aboard, btw!

Offline Statsman1

  • Newbie
  • Posts: 36
  • I'm just a jerk, but a hero is what I want to be.
    • View Profile
Re: Use TIME$ to calculate elapsed time to tenth of a second?
« Reply #3 on: September 23, 2021, 08:28:17 pm »
Thanks!  Happy to be here!

Oooh, now I feel like an idiot.  That example makes it so obviously simple.  Thank you very much, I was obviously taking a far more complicated and useless route.

Good decisions come from experience.
Experience comes from bad decisions.

Offline johnno56

  • Forum Resident
  • Posts: 1270
  • Live long and prosper.
    • View Profile
Re: Use TIME$ to calculate elapsed time to tenth of a second?
« Reply #4 on: September 24, 2021, 04:30:44 am »
Regardless of the "route" taken, as long as it produces the results you seek, can hardly be classified as "useless"... Well that's my philosophy lesson for today... I'm obviously functioning on lower levels of caffeine again... Babbling once again... lol... Oh. By the way... Welcome to the forum...

You are advised to give priority to the colour 'blue'.... Moo Ha Ha....
Logic is the beginning of wisdom.

Offline Statsman1

  • Newbie
  • Posts: 36
  • I'm just a jerk, but a hero is what I want to be.
    • View Profile
Re: Use TIME$ to calculate elapsed time to tenth of a second?
« Reply #5 on: September 24, 2021, 09:29:28 am »
Regardless of the "route" taken, as long as it produces the results you seek, can hardly be classified as "useless"... Well that's my philosophy lesson for today... I'm obviously functioning on lower levels of caffeine again... Babbling once again... lol... Oh. By the way... Welcome to the forum...

You are advised to give priority to the colour 'blue'.... Moo Ha Ha....

My route, tragically, was not producing anything close to what I needed, but I guess I need to follow Occam’s Razor in the future.  I read the TIMER page on the Wiki, and just never extrapolated the difference between two “number of seconds since the most recent midnight.” would be what I was looking for.  I am such a noob here.

Thanks for the welcome, and blue is one of my favorite colours….hope you find all the coffee you can drink!
Good decisions come from experience.
Experience comes from bad decisions.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Use TIME$ to calculate elapsed time to tenth of a second?
« Reply #6 on: September 24, 2021, 10:30:52 am »
One thing to note: If you need *exact* amounts of time, remember to use TIMER’s optional parameter.

For example, this could fail:
T# = TIMER
DO

LOOP UNTIL TIMER = T# + .2

Timer gives us results in fractions of seconds, but not every X amount of seconds.  It might report 0.0001 second passed.. 0.0003 seconds passed….  0.1999 seconds passed….  0.2002 seconds passed….    In this case, it’d never be exactly 0.2 seconds!!

Fix to this is to specify precision:

T# = TIMER(0.1)
DO

LOOP UNTIL TIMER(0.1) = T# + 0.2

That optional parameter is used to set the accuracy that you’re using with the timer statement.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Statsman1

  • Newbie
  • Posts: 36
  • I'm just a jerk, but a hero is what I want to be.
    • View Profile
Re: Use TIME$ to calculate elapsed time to tenth of a second?
« Reply #7 on: September 24, 2021, 11:26:05 am »
Thanks for the tip!  I am, however, basically measuring the interval between, say, the start of a race and the crossing of the finish line.  So reading TIMER at the start and then at the end, and calculating the difference, is basically what I was looking for.  Just didn’t glean it from reading how TIMER worked.

In other news, you guys sure are helpful and responsive!  Much appreciated!

Good decisions come from experience.
Experience comes from bad decisions.

Offline Richard

  • Seasoned Forum Regular
  • Posts: 364
    • View Profile
Re: Use TIME$ to calculate elapsed time to tenth of a second?
« Reply #8 on: September 24, 2021, 11:58:25 am »
@Statsman1

You can measure time intervals down to 100 nanoseconds (refer @SMcNeill high precision timer)

Offline Statsman1

  • Newbie
  • Posts: 36
  • I'm just a jerk, but a hero is what I want to be.
    • View Profile
Re: Use TIME$ to calculate elapsed time to tenth of a second?
« Reply #9 on: September 24, 2021, 12:37:06 pm »
@Statsman1

You can measure time intervals down to 100 nanoseconds (refer @SMcNeill high precision timer)

Thanks for the tip!  That might be TOO precise for what I am measuring, I think 10ths will be enough for the purpose.  If I needed to get down to that level of accuracy, I might be heading for a recreational drug habit…
Good decisions come from experience.
Experience comes from bad decisions.

Offline Qwerkey

  • Forum Resident
  • Posts: 755
    • View Profile
Re: Use TIME$ to calculate elapsed time to tenth of a second?
« Reply #10 on: September 24, 2021, 01:14:27 pm »
You can measure time intervals down to 100 nanoseconds (refer @SMcNeill high precision timer)

@Richard
100 nanoseconds (0.1 microseconds)???  Really?  I don't think the hardware does better than a millisecond(?), and TIMER(0.001) using a Double variable will do 1millisecond accuracy at best.  That's 4 orders of magnitude higher than 100 nanoseconds.

I hope that this is not another simple thing that I have misunderstood.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Use TIME$ to calculate elapsed time to tenth of a second?
« Reply #11 on: September 25, 2021, 04:02:29 am »
One more thing that should be mentioned here: Be careful of midnight reset, if your timer process is important and program execution can occur as the clocks switch over.

At 11:59:59 PM, your timer will be 86399.999999 (or something like that, if my math is off). 

At 12:00:00 AM, the timer resets back to 0.0.

If your code is something like this, it’ll fail:

T# = TIMER
DO
… stuff
LOOP UNTIL TIMER > T# + 1

Basically, the above is a loop that does stuff for 1 second, before it exits the loop.  Problem is, it becomes an endless loop which can never reach the exit condition, if it was run at 11:59:59 PM.  It’s a time bomb hidden in your code!

Easiest solution to this type issue? 

Just plug in and use my ExtendedTimer function instead!  🤩

https://www.qb64.org/forum/index.php?topic=1598.0
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Richard Frost

  • Seasoned Forum Regular
  • Posts: 316
  • Needle nardle noo. - Peter Sellers
    • View Profile
Re: Use TIME$ to calculate elapsed time to tenth of a second?
« Reply #12 on: September 28, 2021, 12:58:39 am »
Steve's extendedtimer function is the ultimate solution, but overkill for most situations.   An easier way to deal with crossing midnite is:


Code: QB64: [Select]
  1. start! = Timer
  2. Do: _Limit 100 '                                       _LIMIT helps prevent CPU overload
  3.     elapsed! = Timer - start!
  4.     If elapsed! < 0 Then elapsed! = elapsed! + 86400 ' must have crossed midnite, add a day
  5.     Locate 1, 1: Print Using "###.##"; elapsed! '      show elapsed time
  6. Loop Until Len(InKey$) '                               any key exits loop
  7. System '                                               return to watching cat videos
  8.  
It works better if you plug it in.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Use TIME$ to calculate elapsed time to tenth of a second?
« Reply #13 on: September 28, 2021, 01:31:33 am »
I write a lot of code which is overkill.  I find it’s more flexibly powerful that away.  💪💪
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline david_uwi

  • Newbie
  • Posts: 71
    • View Profile
Re: Use TIME$ to calculate elapsed time to tenth of a second?
« Reply #14 on: September 28, 2021, 03:10:07 am »
Probably these methods will not work anymore. Many years ago I needed to get short accurate delay. There were two ways to do this:
Read the "time stamp counter" - basically reading the CPU clock using ASM:RDTSC (HEX:0F31) as I was using a 160 MHz pentium the clock rate was 160 MHz.

The other way is to change the clock tick rate:
By setting &H40 and reading from &H46C and &H46D - probably only works with DOS :(

DEF SEG = 0
REM VALUES BELOW FOR CLOCK TICK RATE 10 ms and 1 ms
IH = 46: IL = 156 '10 ms
IH = 4: IL = 169 '1 ms
OUT &H43, &H34
OUT &H40, IL
OUT &H40, IH

WHILE PEEK(&H46C) < 40: WEND 'shortish delay
WHILE PEEK(&H46D) < 16: WEND 'longish delay