QB64.org Forum
Active Forums => QB64 Discussion => Topic started by: Statsman1 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!
-
TIMER returns how many seconds have elapsed since midnight.
PRINT "Time elapsed:"; finish!
- start!
-
Welcome aboard, btw!
-
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.
-
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....
-
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!
-
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.
-
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!
-
@Statsman1
You can measure time intervals down to 100 nanoseconds (refer @SMcNeill high precision timer)
-
@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…
-
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.
-
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
-
Steve's extendedtimer function is the ultimate solution, but overkill for most situations. An easier way to deal with crossing midnite is:
Do:
_Limit 100 ' _LIMIT helps prevent CPU overload elapsed!
= Timer - start!
If elapsed!
< 0 Then elapsed!
= elapsed!
+ 86400 ' must have crossed midnite, add a day System ' return to watching cat videos
-
I write a lot of code which is overkill. I find it’s more flexibly powerful that away. 💪💪
-
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
-
I had a project where laps were measured on 1/4 mile track. They had an old mechanical clock doing it. Lot's of arguments about who had the better times. That argument was settled by a 10 MHZ clock and latching decade counter (inside a dos PC, QB45 was used). Laps were reported to the milisec. But stored to the microsec. There was one instance where two fast cars reported the same timed lap. Normally the first car recorded time counted as first. But that time the second car was just a bit faster. A challenge was made, "We showed how the second car was a couple of 10 thousandnths faster)." Settled the argument. The take away both agreed, overkill to measure beyond milisecs. But we did. After that nobody in pit row made another challenge to stated times. Even the NASCAR guys were impressive when they came by during there 1/4 track road show. "They needed timed laps".