Page 1 of 1
ElapsedMilliseconds() = 0
Posted: Thu Aug 11, 2022 9:36 am
by SPH
Hello,
in time, ElapsedMilliseconds() gave a number other than 0 when running a code. Now watch:
Code: Select all
Delay(100)
Debug ElapsedMilliseconds()
Delay(100)
Debug ElapsedMilliseconds()
I'm not going to call it a bug but it can give different results for codes that we would recompile in PB6...

Re: ElapsedMilliseconds() = 0
Posted: Thu Aug 11, 2022 9:48 am
by Janni
I think you need to call it first for timer to start. You mean this is different from previous versions of PB ?
Code: Select all
ElapsedMilliseconds()
Delay(100)
Debug ElapsedMilliseconds()
Delay(100)
Debug ElapsedMilliseconds()
Re: ElapsedMilliseconds() = 0
Posted: Thu Aug 11, 2022 9:58 am
by Demivec
SPH wrote: Thu Aug 11, 2022 9:36 amI'm not going to call it a bug but it can give different results for codes that we would recompile in PB6...
It was changed a few versions back. The Manual gives a hint that ElapsedMilliseconds() is not for the actual time but for timing the 'time between calls' also know as the elapsed milliseconds

.
Fred gives an explanation in the following thread and states that there were changes made for the Windows OS to bring it into agreement with Linux and the Mac OS:
https://www.purebasic.fr/english/viewtopic.php?p=503797 .
Re: ElapsedMilliseconds() = 0
Posted: Fri Aug 12, 2022 10:16 am
by ozzie
Demivec wrote: Thu Aug 11, 2022 9:58 am
It was changed a few versions back. The Manual gives a hint that ElapsedMilliseconds() is not for the actual time but for timing the 'time between calls' also know as the elapsed milliseconds.
Oops! I didn't know that and it breaks some of my logic. I assumed it was elapsed milliseconds since the program started. I've now added this macro:
Code: Select all
Macro myMilliseconds()
(qMyElapsedMilliseconds + ElapsedMilliseconds())
EndMacro
where qMyElapsedMilliseconds.q is a global variable, and changed all other ElapsedMilliseconds() to myMilliseconds().
Re: ElapsedMilliseconds() = 0
Posted: Fri Aug 12, 2022 12:12 pm
by Axolotl
And on windows you can use this to get what you expected (milliseconds since program start)
Code: Select all
Debug "GetTickCount_()" ; == sample output
Debug GetTickCount_() ; == 672279453
Delay(500) ;
Debug GetTickCount_() ; == 672279953
Debug "ElapsedMilliseconds()" ;
Debug ElapsedMilliseconds() ; == 0
Delay(500) ;
Debug ElapsedMilliseconds() ; == 500
Re: ElapsedMilliseconds() = 0
Posted: Fri Aug 19, 2022 11:39 pm
by Olli
When I discover this new characteristic, I found it bad.
i.e. to select a random seed.
But if we use it to synchronize a loop, and want to use the maximum of wait time to execute background non-threaded task, the algo is simplified.
ozzie demonstrated it a few before. The zero initial value prevents the program from having a non real initial duration in a loop which required an additionnal ghost condition, useful only one time in a strategic loop.
Code: Select all
While on
If nonFirstLoop = 0
nonFirstLoop = 1
tIni = ElapsedMilliseconds()
EndIf
t1 = ElapsedMilliseconds() - tIni
dt21 = t1 - t2
mainLoopOps()
t2 = ElapsedMilliseconds() - tIni
dt12 = t2 - t1
If dt12 < msPerLoop ; without tIni, the first loop has no delay because this safety condition "thinks" the previous delay (which really does not exist) would be very VERY long...
Delay(msPerLoop - dt12)
EndIf
Wend
...so an initial op speed would cause a very bad initial result.
i.e. a running hero would start with a very big and unreal speed.
Now, we have :
Code: Select all
While on
t1 = ElapsedMilliseconds()
dt21 = t1 - t2
mainLoopOps()
t2 = ElapsedMilliseconds()
dt12 = t2 - t1
If dt12 < msPerLoop
Delay(msPerLoop - dt12)
EndIf
Wend
Re: ElapsedMilliseconds() = 0
Posted: Fri Aug 19, 2022 11:55 pm
by jacdelad
Code: Select all
While on
t1=ElapsedMilliseconds()+msPerLoop
mainLoopOps()
If ElapsedMilliseconds()<t1
Delay(t1-ElapsedMilliseconds())
EndIf
Wend
...but don't use Delay for GUI waittimes. Use a timer instead.
Re: ElapsedMilliseconds() = 0
Posted: Sat Aug 20, 2022 2:28 pm
by NicTheQuick
Olli wrote: Fri Aug 19, 2022 11:39 pm
When I discover this new characteristic, I found it bad.
i.e. to select a random seed.
You don't have to set a random seed by yourself because Purebasic automatically ensures that the seed is different on every program start. You only need RandomSeed() if you really want to set it to a specific value.