ElapsedMilliseconds() returns zero?

Just starting out? Need help? Post your questions and find answers here.
User avatar
pdwyer
Addict
Addict
Posts: 2813
Joined: Tue May 08, 2007 1:27 pm
Location: Chiba, Japan

ElapsedMilliseconds() returns zero?

Post by pdwyer »

Am I doing something wrong?

debug ElapsedMilliseconds() returns zero

MyVal.q = ElapsedMilliseconds()

or whatever.

Old code I have is doing weird stuff due to this suddenly... should I install an old version of PB to see if it's a version thing or can someone see why I'm just lacking in sleep and doing something dumb...
This can't be right, I must be going mad

x86 and x64 the same

Thanks
Paul Dwyer

“In nature, it’s not the strongest nor the most intelligent who survives. It’s the most adaptable to change” - Charles Darwin
“If you can't explain it to a six-year old you really don't understand it yourself.” - Albert Einstein
User avatar
TI-994A
Addict
Addict
Posts: 2512
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: ElapsedMilliseconds() returns zero?

Post by TI-994A »

The PureBasic Manual - ElapsedMilliseconds() wrote:Remarks
The absolute value returned is of no use since it varies depending on the operating system. Instead, this function should be used to calculate time differences between multiple ElapsedMilliseconds() calls.

Code: Select all

For i = 0 To 10
  Debug ElapsedMilliseconds()
  Delay(1000)
Next
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
User avatar
microdevweb
Enthusiast
Enthusiast
Posts: 179
Joined: Fri Jun 13, 2014 9:38 am
Location: Belgique

Re: ElapsedMilliseconds() returns zero?

Post by microdevweb »

Hello,
That depend when you call this function

See this example :

Code: Select all

myval.q = ElapsedMilliseconds()
Debug myval ; it's normal because the program is started now
; i run a loop for some time
For i = 0 To 1000000
  ; i don't do anything
Next
myval = ElapsedMilliseconds()
Debug myval ; in my case print 85
Use Pb 5.73 lst and Windows 10

my mother-language isn't english, in advance excuse my mistakes.
Marc56us
Addict
Addict
Posts: 1477
Joined: Sat Feb 08, 2014 3:26 pm

Re: ElapsedMilliseconds() returns zero?

Post by Marc56us »

In fact it's the description of the function in the help that is badly formulated (and it's worse in the french help « Renvoie le nombre de millisecondes qui se sont écoulées depuis le démarrage de l'ordinateur. » (Translation: Returns the number of milliseconds that have elapsed since the computer was started. )) :?

IMHO, the good description should be:

:arrow: Returns the number of milliseconds that have elapsed since the function was first called in the program.

Code: Select all

Debug ElapsedMilliseconds() 
Delay(1000)
Debug ElapsedMilliseconds() 
Delay(2000)
Debug ElapsedMilliseconds() 
3000 OK

:wink:

Edit: Kenmo is right, I seem to remember that the behaviour changed a few years ago but the help hasn't been updated.
Last edited by Marc56us on Fri Jan 24, 2020 2:59 pm, edited 2 times in total.
User avatar
kenmo
Addict
Addict
Posts: 1967
Joined: Tue Dec 23, 2003 3:54 am

Re: ElapsedMilliseconds() returns zero?

Post by kenmo »

You're not mad, the behavior changed a few years ago.

It used to return something like <time the system's been running> but now it's <time relative to the first call, t=0> so it's only useful for relative timing.

I think the reason for changing was: the original behavior was not reliable across systems, or not consistent across OSes (?) It's not described in the version history, but it might be discussed on this forum.
User avatar
kenmo
Addict
Addict
Posts: 1967
Joined: Tue Dec 23, 2003 3:54 am

Re: ElapsedMilliseconds() returns zero?

Post by kenmo »

It changed some time between 5.44 and 5.60, here's Fred's comment in 2017
viewtopic.php?f=13&t=67989#p503543

The English help was updated in 2019, but the French needs to be updated. Maybe others too
viewtopic.php?f=4&t=73191
User avatar
pdwyer
Addict
Addict
Posts: 2813
Joined: Tue May 08, 2007 1:27 pm
Location: Chiba, Japan

Re: ElapsedMilliseconds() returns zero?

Post by pdwyer »

ahhhhh

okay. The actual value was never of much use except in comparing so I didn't notice that detail about the first call being set to 0
Paul Dwyer

“In nature, it’s not the strongest nor the most intelligent who survives. It’s the most adaptable to change” - Charles Darwin
“If you can't explain it to a six-year old you really don't understand it yourself.” - Albert Einstein
User avatar
Josh
Addict
Addict
Posts: 1183
Joined: Sat Feb 13, 2010 3:45 pm

Re: ElapsedMilliseconds() returns zero?

Post by Josh »

Since a few versions PB uses QueryPerformanceCounter() instead of GetTickCount() for ElapsedMilliseconds() .

This has the following advantages:
  • GetTickCount() only returned results in steps of 32ms (or something similar). QueryPerformanceCounter() is much more accurate.
  • The result of GetTickCount() is a long, which can lead to problems with server programs that run longer time, because a long is full after a few weeks. The result of QueryPerformanceCounter() is a quad, so it can run for several hundred thousand years.
sorry for my bad english
BarryG
Addict
Addict
Posts: 3292
Joined: Thu Apr 18, 2019 8:17 am

Re: ElapsedMilliseconds() returns zero?

Post by BarryG »

Correct. GetTickCount() only returns valid times for approx 49 days. These days, PCs are left running a lot longer than that.
Olli
Addict
Addict
Posts: 1071
Joined: Wed May 27, 2020 12:26 pm

Re: ElapsedMilliseconds() returns zero?

Post by Olli »

I am a few late there. I discover this change only now...
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: ElapsedMilliseconds() returns zero?

Post by mk-soft »

BarryG wrote: Sat Jan 25, 2020 12:48 am Correct. GetTickCount() only returns valid times for approx 49 days. These days, PCs are left running a lot longer than that.
For long-term measurement, the date functions are also used. For runtime measurement, GetTickCount was also sufficient. The statement that it is a problem that this value is only a long and it leads to problems with an overflow is completely wrong and had already upset me at the time of the discussion.
The different calculation of the time is also always correct at the start before and at the end with an overflow of the long value. (Of course, only with a difference of less than about 47 days).
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Post Reply