ElapsedMilliseconds() = 0

Everything else that doesn't fall into one of the other PB categories.
User avatar
SPH
Enthusiast
Enthusiast
Posts: 268
Joined: Tue Jan 04, 2011 6:21 pm

ElapsedMilliseconds() = 0

Post 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... :idea:
http://HexaScrabble.com/
!i!i!i!i!i!i!i!i!i!
!i!i!i!i!i!i!
!i!i!i!
//// Informations ////
Portable LENOVO ideapad 110-17ACL 64 bits
Version de PB : 5.73LTS - 32 bits
User avatar
Janni
Enthusiast
Enthusiast
Posts: 127
Joined: Mon Feb 21, 2022 5:58 pm
Location: Norway

Re: ElapsedMilliseconds() = 0

Post 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()
Spec: Linux Mint 20.3 Cinnamon, i7-3770K, 16GB RAM, RTX 2070 Super
User avatar
Demivec
Addict
Addict
Posts: 4086
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: ElapsedMilliseconds() = 0

Post 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... :idea:
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 :wink: .

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 .
ozzie
Enthusiast
Enthusiast
Posts: 429
Joined: Sun Apr 06, 2008 12:54 pm
Location: Brisbane, Qld, Australia
Contact:

Re: ElapsedMilliseconds() = 0

Post 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().
Axolotl
Enthusiast
Enthusiast
Posts: 435
Joined: Wed Dec 31, 2008 3:36 pm

Re: ElapsedMilliseconds() = 0

Post 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            
Mostly running PureBasic <latest stable version and current alpha/beta> (x64) on Windows 11 Home
Olli
Addict
Addict
Posts: 1071
Joined: Wed May 27, 2020 12:26 pm

Re: ElapsedMilliseconds() = 0

Post 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
User avatar
jacdelad
Addict
Addict
Posts: 1431
Joined: Wed Feb 03, 2021 12:46 pm
Location: Planet Riesa
Contact:

Re: ElapsedMilliseconds() = 0

Post 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.
PureBasic 6.04/XProfan X4a/Embarcadero RAD Studio 11/Perl 5.2/Python 3.10
Windows 11/Ryzen 5800X/32GB RAM/Radeon 7770 OC/3TB SSD/11TB HDD
Synology DS1821+/36GB RAM/130TB
Synology DS920+/20GB RAM/54TB
Synology DS916+ii/8GB RAM/12TB
User avatar
NicTheQuick
Addict
Addict
Posts: 1224
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: ElapsedMilliseconds() = 0

Post 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.
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
Post Reply