Correct that ElapsedTime() never resets to zero?

Just starting out? Need help? Post your questions and find answers here.
Oso
Enthusiast
Enthusiast
Posts: 595
Joined: Wed Jul 20, 2022 10:09 am

Correct that ElapsedTime() never resets to zero?

Post by Oso »

Am I correct in saying that ElapsedTime() never resets during a running process, unless it exceeds the limit of the variable? In other words, it represents the period of time in milliseconds that the process has been running?

When I wrote the below timer code some weeks ago, I deliberately tested if ElapsedMilliseconds() has cycled back round to zero, which I think I've done correctly (first conditional statement). But just thinking about it properly now, if the limit of 64-bit integer is 9,223,372,036,854,775,807 then divided by the number of milliseconds in a day, that would be 1,067,519 days. :D

Just wanted to check really, is there any way that ElapsedMilliseconds() could be reset, that I'm not aware of?

Code: Select all

    ; **
    ; ** Perform periodic reconnection attempts
    ; **
    timernow.i = ElapsedMilliseconds()                                  ; Time now
    If timernow.i < timerlast.i                                         ; Deal with ElapsedMilliseconds() time having reset back to zero
      timerlast.i = 0                                                   ; Reset the last time to deal with this case
    EndIf
    If timernow.i > timerlast + timerint.i                              ; Check for elapsed interval period 
      timerlast.i = timernow.i                                          ; Set the last time to now
User avatar
mk-soft
Always Here
Always Here
Posts: 6329
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Correct that ElapsedTime() never resets to zero?

Post by mk-soft »

No,

A performance timer is started the first time it is used. To get a new starting point you have to buffer the current value.
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
Oso
Enthusiast
Enthusiast
Posts: 595
Joined: Wed Jul 20, 2022 10:09 am

Re: Correct that ElapsedTime() never resets to zero?

Post by Oso »

mk-soft wrote: Sat Dec 02, 2023 12:15 pm No, A performance timer is started the first time it is used. To get a new starting point you have to buffer the current value.
Great, that's fine then, thanks for that. I seem to remember testing this code some weeks ago but I'm not sure how I did it! I think I must have added a fake time to it, to exceed the limit. Anyway, I don't think anyone's computer is going to run continuously for 1 million days. :shock:
User avatar
mk-soft
Always Here
Always Here
Posts: 6329
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Correct that ElapsedTime() never resets to zero?

Post by mk-soft »

An overflow should also not be a problem as the quad time is defined as Signed. This means that the difference is always correct when calculating with overflow, as long as the difference fits in a quad

Code: Select all

Global time.q, lasttime, diff.q

; Simulate high time
time = $7FFFFFFFFFFFFFFF - 200

lasttime = time
For i = 1 To 10
  time + 40
  diff = time - lasttime
  Debug "" + time + " - " + diff
  lasttime = time
Next
----

This is also not a fault if an overflow occurs. Therefore, you must always make sure that your own calculations fit into the variable type.

Code: Select all

;- Addition with overflow check

Procedure addLongOvf(*result.long, a.l, b.l)
  *result\l = a + b;
  If(a > 0 And b > 0 And *result\l < 0)
    ProcedureReturn -1;
  EndIf
  If(a < 0 And b < 0 And *result\l > 0)
    ProcedureReturn -1;
  EndIf
  ProcedureReturn 0 ;
EndProcedure

Define a.l
Define b.l
Define c.l

a.l = $7FFFFFFF - 10
b.l = 20
r1 = addLongOvf(@c, a, b)
If r1
  Debug "Overflow"
EndIf
Debug c

a.l = $80000000 + 10
b.l = -20
r1 = addLongOvf(@c, a, b)
If r1
  Debug "Overflow"
EndIf
Debug c
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
Oso
Enthusiast
Enthusiast
Posts: 595
Joined: Wed Jul 20, 2022 10:09 am

Re: Correct that ElapsedTime() never resets to zero?

Post by Oso »

mk-soft wrote: Sat Dec 02, 2023 12:45 pm An overflow should also not be a problem as the quad time is defined as Signed. This means that the difference is always correct when calculating with overflow, as long as the difference fits in a quad
Ah yes, I see. It's perpetual then, excellent.
juergenkulow
Enthusiast
Enthusiast
Posts: 581
Joined: Wed Sep 25, 2019 10:18 am

Re: Correct that ElapsedTime() never resets to zero?

Post by juergenkulow »

After more than 100000 years of program runtime (3551548724241346 ms), an overflow to -3551548724241346 would occur on my Windows.
Please ask your questions, because switch on the cognition apparatus decides on the only known life in the universe.Wersten :DDüsseldorf NRW Germany Europe Earth Solar System Flake Bubble Orionarm
Milky Way Local_Group Virgo Supercluster Laniakea Universe
User avatar
jacdelad
Addict
Addict
Posts: 2035
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Re: Correct that ElapsedTime() never resets to zero?

Post by jacdelad »

So after about 200000 years it should be zero again. :mrgreen:
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
User avatar
Demivec
Addict
Addict
Posts: 4282
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Correct that ElapsedTime() never resets to zero?

Post by Demivec »

jacdelad wrote: Mon Dec 04, 2023 5:07 pm So after about 200000 years it should be zero again. :mrgreen:
So that could lead to a Y.2M type of bug? :P
juergenkulow
Enthusiast
Enthusiast
Posts: 581
Joined: Wed Sep 25, 2019 10:18 am

Re: Correct that ElapsedTime() never resets to zero?

Post by juergenkulow »

If:

Code: Select all

Debug Date(2039,1,1,0,0,0)
;-1
Then any question about overflows is very justified.
By the way: 225238 years
Post Reply