FreeBasic's Timer

General FreeBASIC programming questions.
Post Reply
neil
Posts: 592
Joined: Mar 17, 2022 23:26

FreeBasic's Timer

Post by neil »

This timer test has been working OK most of the time. Why does the speed double sometimes?
It seems like something is interfering with FreeBasic's timer itself.
deltarho mentioned something about the gcc interfering with the loop in another program.

Code: Select all

Dim AS Double starttime,stoptime,i
Dim As long tdelay,speed,n
Screen 12

'' pick a speed  (0 - 1000)
'' A large number for a longer delay
speed = 100

Print "Speed = ";speed 
speed = (speed * 2) + 1
tdelay = speed

starttime = timer
For i = 1 TO 10000:Next
stoptime = timer
tdelay = (tdelay * 5) / (stoptime - starttime)
print "Delay Loop Counter = ";tdelay

''speed test using a delay loop
for i = 1 to 79
Locate 4,1:Print "Speed Test"
Locate 5,i: print "X";
for n = 1 to tdelay:next
next

sleep
Last edited by neil on May 19, 2023 9:33, edited 1 time in total.
neil
Posts: 592
Joined: Mar 17, 2022 23:26

Re: FreeBasic's Timer

Post by neil »

I ran the timer test code 10 times.

I got very close to this 8 times.
Delay Loop Counter = 36027996

I got this 2 times
Delay Loop Counter = 11181102

This is quite a difference.
neil
Posts: 592
Joined: Mar 17, 2022 23:26

Re: FreeBasic's Timer

Post by neil »

I did more timer tests.

I got this 10 times in a row.
Delay Loop Counter = 15670169

What is going on with the timer?
hhr
Posts: 208
Joined: Nov 29, 2019 10:41

Re: FreeBasic's Timer

Post by hhr »

I suspect it's because of multitasking.
The program gets time slots assigned by the operating system.
Only when the program is active, it can read the timer value.
neil
Posts: 592
Joined: Mar 17, 2022 23:26

Re: FreeBasic's Timer

Post by neil »

@hhr
You could be correct. This could mean using the timer to compare speed. A slower algorithm could appear to be faster, The faster one could appear to be slower. Who Knows?
srvaldez
Posts: 3379
Joined: Sep 25, 2005 21:54

Re: FreeBasic's Timer

Post by srvaldez »

neil, timing an empty loop or a loop which result is not used outside the timing loop is not a good idea, it could simply be removed by the compiler depending on the options used
but a loop like For i = 1 TO 10000:Next is simply too short, the timer resolution is not nearly high enough for something that brief
fxm
Moderator
Posts: 12110
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: FreeBasic's Timer

Post by fxm »

An example of a small 'delay()' procedure (provided delay time in milliseconds) that seems fairly accurate throughout its full range of use from 1 millisecond, given the associated test example:
(for 1 ms requested, only some values up to 2 times larger on a very large number of calls)

Code: Select all

Sub delay(Byval t As Ulong)  '' delay 't' expressed in milliseconds
    Dim As Double t1 = Timer
    Dim As Double t2
    Dim As Double t3 = t / 1000
    If t > 32 Then
        Sleep t - 32, 1
    End If
    Do
        t2 = Timer
        If t2 < t1 Then t1 = t2
    Loop Until t2 >= t1 + t3
End Sub

'------------------------------------------------------------------

Dim As Double t
Dim As Ulong t0 = 1000

For N As Integer = 1 To 4
    Print "Delay= "; t0; " ms"
    For I As Integer = 1 To 4
        t = Timer
        delay(t0)
        Print Using"#.###### s"; Timer - t
    Next I
    Print
    t0 /= 10
Next N

Sleep
Delay= 1000 ms
1.000065 s
1.000008 s
1.000006 s
1.000010 s

Delay= 100 ms
0.100005 s
0.100007 s
0.100004 s
0.100008 s

Delay= 10 ms
0.010002 s
0.010001 s
0.010007 s
0.010003 s

Delay= 1 ms
0.001001 s
0.001001 s
0.001001 s
0.001001 s
Last edited by fxm on May 24, 2023 13:27, edited 3 times in total.
Reason: Large-grained threshold of 'delay()' adjusted to 32ms (instead of 300ms) as proposed by deltarho.
neil
Posts: 592
Joined: Mar 17, 2022 23:26

Re: FreeBasic's Timer

Post by neil »

@fxm
Thanks it works. Now I need to figure out how to make a regulator function with it.
neil
Posts: 592
Joined: Mar 17, 2022 23:26

Re: FreeBasic's Timer

Post by neil »

@srvaldez
I tried it with your suggestion "For i = 1 TO 10000:Next" and increased it to larger number and it seemed to work much better.
Thanks for your tip.
hhr
Posts: 208
Joined: Nov 29, 2019 10:41

Re: FreeBasic's Timer

Post by hhr »

I have a critical question.
The accuracy of the values could be an illusion.
If you measure the accuracy of an instrument with the same instrument, you can only capture the random error.
One cannot capture the systematic error this way.
One can determine the accuracy of an instrument only with a more accurate instrument, not with the instrument itself.

I have built a count variable into the delay loop. This count variable shows different values.

However, if you want to prevent a graphics program from running at a fluctuating speed, you can very well use the timer.
As I understood it, this is done with the function regulate.

Code: Select all

function Delay(dt as double) as ulongint
   dim as ulongint i
   dim as double t=timer+dt
   do while timer < t
      i+=1
   loop
   return i
end function

dim as double t1,t2
dim as ulongint i

do
   i=0
   t1=timer
   i=Delay(0.001)
   t2=timer
   print t2-t1;tab(30);i,bin(i,32)
loop until getkey=27
Translated with www.DeepL.com/Translator (free version)
neil
Posts: 592
Joined: Mar 17, 2022 23:26

Re: FreeBasic's Timer

Post by neil »

What I meant about a regulator function was that a graphics program would run at the same speed on any computer.
A slow PC or a fast PC. Set the speed once the way you like it. Then it should adjust itself and run at the same speed on any PC.
I am not sure how to do this using the timer.
hhr
Posts: 208
Joined: Nov 29, 2019 10:41

Re: FreeBasic's Timer

Post by hhr »

You should ask deltarho, dodicat, srvaldez...
They have done something like this before.
Imortis
Moderator
Posts: 1924
Joined: Jun 02, 2005 15:10
Location: USA
Contact:

Re: FreeBasic's Timer

Post by Imortis »

http://games.freebasic.net/BASICGaming/Issue9/index.html#tutorial1
See the article at the above link. It will explain how to do what you are looking to do. I found it pretty quickly in the GameDev section of the forum.
fxm
Moderator
Posts: 12110
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: FreeBasic's Timer

Post by fxm »

hhr wrote: May 19, 2023 19:10 I have a critical question.
The accuracy of the values could be an illusion.
If you measure the accuracy of an instrument with the same instrument, you can only capture the random error.

For modern processors, the accuracy of the internal timer counter is tied to the processor clock, so very good.
The biggest part of uncertainty relates to the sampling moment of the internal counter from user code (via the TIMER keyword) and not on the read value itself.
So if the accuracy thus measured seems good, it really is.
hhr
Posts: 208
Joined: Nov 29, 2019 10:41

Re: FreeBasic's Timer

Post by hhr »

@fxm
That's how I see it too, you have to know how to interpret things.
Post Reply