Calculate estimated time and end time Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Albireo
Posts: 1748
Joined: 16 Oct 2013, 13:53

Calculate estimated time and end time

18 Apr 2021, 07:43

My wish is to show information about a program run that contains a loop.

Code: Select all

Loop 60
{   ; Do something
   Sleep 1000
}
For example with this information .:
  • Start clock 13:14:15 (for example)
  • (Loop time) 1,34 seconds or 1:14 (One minute and fourteen seconds)
  • Estimated time 1,34 seconds or 36 seconds or... (Usually quite close to estimated time)
  • Estimated Stop clock 13:17:19
I have tried, but failed ...

Code: Select all

#SingleInstance force
iterations  := "60"
runTime := []

runTime["startMS"] := A_TickCount			; 488259218
runTime["countMS"] := runTime.Start			; 488259218

tmpTime := runTime["staClock"] := A_Now	; 20210418118311
FormatTime tmpClock, tmpTime, HH:mm:ss		; 11:47:11
runTime["staClockShow"] := tmpClock			; 11:47:11

Loop % iterations
{	runTime["stoClock"]	:= runTime.staClock	; 20210418118311

	; runTime["staTime"]	:= A_TickCount
	Sleep 500
	; runTime["stoTime"]	:= A_TickCount
	; runTime["iterTime"]	:= runTime.stoTime - runTime.staTime
	
	runTime["estimated"] := (A_TickCount - runTime.startMS) / A_Index
	runTime["totTime"]	:= runTime.estimated * iterations
	tmpTime := Round((runTime.totTime/1000), 0)
	runTime.stoClock += tmpTime, seconds
	tmpTime := runTime.stoClock
	FormatTime tmpClock, tmpTime, HH:mm:ss
	runTime["stoClockShow"] := tmpClock
	
	ToolTip % runTime.staClockShow "`n" runTime.estimated "`n" runTime.stoClock "`n" runTime.stoClockShow
	; ToolTip % runTime.staClockShow "`n" runTime.estimated "`n" runTime.stoClock "`n" runTime.stoClockShow "`n" tmpTime "`n" runTime.stoClock
	; ToolTip % A_Index "`n" runTime.iterTime "`n" runTime.estimated "`n" runTime.totTime
}

MsgBox ,, Rad %A_LineNumber% -> %A_ScriptName%, % "Ready! `n" A_TickCount - runTime.startMS
ExitApp
User avatar
mikeyww
Posts: 26848
Joined: 09 Sep 2014, 18:38

Re: Calculate estimated time and end time

18 Apr 2021, 07:48

See?

Code: Select all

runTime := []
runTime["startMS"] := A_TickCount			; 488259218
runTime["countMS"] := runTime.Start			; Not 488259218
MsgBox, 48, Error, % "#" runTime.startMS "#`n#"
                       . runTime.Start   "#"
Albireo
Posts: 1748
Joined: 16 Oct 2013, 13:53

Re: Calculate estimated time and end time

18 Apr 2021, 09:34

You have right!
(but I don't use this row later in my try.)

One of my problems is found in these lines

Code: Select all

runTime.stoClock += tmpTime, seconds
	tmpTime := runTime.stoClock
	FormatTime tmpClock, tmpTime, HH:mm:ss
runTime.stoClock shows the desired time (like 20210418163174)
but FormatTime tmpClock, tmpTime, HH:mm:ss doesn't
For some reason it seems to increase every second .
User avatar
mikeyww
Posts: 26848
Joined: 09 Sep 2014, 18:38

Re: Calculate estimated time and end time

18 Apr 2021, 09:51

AHK commands use literal strings, so you can use % when you are specifying a variable.

Code: Select all

FormatTime, tmpClock, %tmpTime%, HH:mm:ss
Explained: FormatTime, example #2
Albireo
Posts: 1748
Joined: 16 Oct 2013, 13:53

Re: Calculate estimated time and end time

18 Apr 2021, 17:18

Thank you!
Developed the solution with a GUI
the Clock Ready at is not shown all time one run it was shown next not (I don't know why).

To check the running time, is this maybe the only way? (I assume a date and set the clock to 00:00:00)
runTime["totTimeShow"] := "20210418000000"
runTime["totRunTime"] := Round(runTime.estimated * oTest.count() / 1000, 0)
runTime.stoClock += runTime.totRunTime, Seconds


Do not know if I complicate the solution, but a few lines that do the job, produce very many instructions to get control of the program run

Code: Select all

#SingleInstance force

itrNumber = 30
oTest := []

runTime := []
runTime["startMS"] := A_TickCount						; 488259218
runTime["staClock"] := A_Now								; 20210418118311
FormatTime tmpClock, % runTime.staClock, HH:mm:ss	; 11:47:11
runTime["staClockShow"] := tmpClock						; 11:47:11
runTime["totTimeShow"] := "20210418000000"			; 20210418118311

Loop % itrNumber
	oTest.push("A_Index")
; MsgBox ,, Rad %A_LineNumber% -> %A_ScriptName%, % oTest.count() 

Gui 1: Color, White
Gui 1: Font, s12 cBlue Bold, Verdana
Gui 1: Add, Text, x0 y10 h20 w300 +center, Test of run time clock

Gui 1: Font, s10 cBlue Normal, Verdana
Gui 1: Add, Text, x20 y50 h20 w150 +Left, Start clock . .`t.:
Gui 1: Add, Text, xp yp+25 h20 w150 +Left, Estimated . .`t.:
Gui 1: Add, Text, xp yp+25 h20 w150 +Left, Runtime . . . . . . .`t.:
Gui 1: Add, Text, xp yp+25 h20 w150 +Left, Ready at . .`t.:

Gui 1: Font, s10 cGreen Bold, Verdana
Gui 1: Add, Text, x140 y50 h20 w70	+Right vTimeStart, % runTime.staClockShow
Gui 1: Add, Text, xp yp+25 h20 w70	+Right vProcTime
Gui 1: Add, Text, xp yp+25 h20 w70	+Right vTotRun
Gui 1: Add, Text, xp yp+25 h20 w70	+Right vTimeReady

Gui 1: Font, s10 cBlue Normal, Verdana
Gui 1: Add, Text, x220 y75 h20 w200 +Left, sek
Gui 1: Add, Text, xp yp+25 h20 w200 +Left, sek


Gui 1: Font, s12 cGreen Bold, Verdana
Gui 1: Add, Text, x0 y170 h20 w300 +center vLev, Some information

Gui 1: Add, Progress, x50 y200 w200 h25 cAqua BackgroundSilver vMyProgress
Gui 1: Font, s10 cBlue Bold, Verdana
; Gui 1: Font, s12 cBlue, Verdana
Gui 1: Add, Text, xp yp+3 h20 w200 +center +BackgroundTrans vProgTxt
Gui 1: Show, h250 w300
; Return	; Stopppar vidare analys

Loop % oTest.count()
{	runTime["stoClock"] := runTime.staClock			; 20210418118311
	
	; runTime["staTime"]	:= A_TickCount
	GuiControl 1:, MyProgress, % Round(100 * A_Index / oTest.count())
	GuiControl 1:, ProgTxt, % "( " A_Index " / " oTest.count() " )"
	Sleep 1000
	; runTime["stoTime"]	:= A_TickCount
	; runTime["iterTime"]	:= runTime.stoTime - runTime.staTime

	; Estimated time
	runTime["estimated"] := (A_TickCount - runTime.startMS) / A_Index
	runTime["estShow"] 	:= Round(runtime.estimated/1000, 2)	; eg. 1.02 sek
	
	; Run time
	runTime.totRun := Round((A_TickCount - runTime.startMS)/1000, 1)

	; MsgBox ,, Rad %A_LineNumber% -> %A_ScriptName%, % runTime.stoClock "`n" runTime.totRunTime "`n" tmpClock "`n" runTime.stoClockShow "`n" runTime.stoClock


	; Program ready (estimated)
	runTime["totRunTime"] := Round(runTime.estimated * oTest.count() / 1000, 0)
	runTime.stoClock	+= runTime.totRunTime, Seconds
	FormatTime tmpClock, % runTime.stoClock, HH:mm:ss
	runTime["stoClockShow"] := tmpClock
	
	; MsgBox ,, Rad %A_LineNumber% -> %A_ScriptName%, % runTime.stoClock "`n" runTime.totRunTime "`n" tmpClock "`n" runTime.stoClockShow "`n" runTime.stoClock
	
	GuiControl 1:, ProcTime,	% runTime.estShow
	GuiControl 1:, TotRun,		% runTime.totRun
	GuiControl 1:, TimeReady,	% runTime.stoClockShow
}

Return

ESC:: ExitApp
User avatar
mikeyww
Posts: 26848
Joined: 09 Sep 2014, 18:38

Re: Calculate estimated time and end time

18 Apr 2021, 17:42

Sorry I do not have time to debug the rest of the script. Others may be able to help. If you are trying to make a stopwatch, then it looks overly complicated; examples are available on the forum, I believe.

I did notice a strange line:

Code: Select all

oTest.push("A_Index")
It looks like you are pushing the actual string, A_Index, onto the stack. Demo is below.

Code: Select all

oTest := []
Loop, 5
 oTest.push("A_Index")
MsgBox, % oTest.1
Albireo
Posts: 1748
Joined: 16 Oct 2013, 13:53

Re: Calculate estimated time and end time

19 Apr 2021, 02:08

Thank you for your time!
You are observant, (I just created an array of nonsense) to use an array in the main program.

This is the only way I came up with, to be able to show start time and estimated end time as clock (eg 08:42:00 / 08:42:53)
At the same time (in parallel) the run time is calculated with A_TickCount.
Becauese I can´t do that
FormatTime runTime.stoClockShow, % runTime.stoClock, HH:mm:ss

it must be this solution .:
FormatTime tmpClock, % runTime.stoClock, HH:mm:ss
runTime["stoClockShow"] := tmpClock


And the way to count the run time (as I mentioned before) I must add a fake date
To check the running time, is this maybe the only way? (I assume a date and set the clock to 00:00:00)
runTime["totTimeShow"] := "20210418000000"
runTime["totRunTime"] := Round(runTime.estimated * oTest.count() / 1000, 0)
runTime.stoClock += runTime.totRunTime, Seconds


It works (but I thought it was complicated to use)
Rohwedder
Posts: 7610
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Calculate estimated time and end time  Topic is solved

19 Apr 2021, 03:54

Hallo,
I made 3 functions to simplify time calculations:

Code: Select all

Clock = 10:00:00
Clock := DateAdd(Clock, 1000, "Seconds", "HH:mm:ss")
MsgBox,% "Clock: " Clock "`n1000 Seconds later"
; Clock: 10:16:40
; 1000 Seconds later

Date1 = 21.02.11
Date2 = 21.04.02
Diff := DateDiff(Date1, Date2, "Days", "yy.MM.dd")
MsgBox,% Diff " Days between " Date1 " and " Date2
; 50 Days between 21.02.11 and 21.04.02

;XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
DateAdd(Date, Time, TimeUnits, Format:="yyyyMMddHHmmss")
{
    Date := TimeStamp(Date, Format)
    EnvAdd, Date,% Time,% TimeUnits
    FormatTime, Date,% Date,% Format
    Return, Date
}
DateDiff(Date1, Date2, TimeUnits, Format:="yyyyMMddHHmmss")
{
    Date2 := TimeStamp(Date2, Format)
    EnvSub, Date2,% TimeStamp(Date1),% TimeUnits
    Return, Date2
}
TimeStamp(Date, Format:="")
{
    Static Noon := A_Year A_MM A_DD 120000, Down := 7060504030201, FOld                                                          
    FormatTime, D,% 8 Down,% Format?FOld:=Trim(Format):FOld  
    For all, V in ["Date","D"], Date := Trim(Date) ; add missing leading zeros
        %V% := RegExReplace(%V%, "(?<=^|\D)(\d)(?=\D|$)", "0$1")
    Loop, Parse,% (0 Down, N := Noon) ; today's noon
        IF A_LoopField And Pos := InStr(D, A_LoopField)
            N := SubStr(N,1,A_Index-2) SubStr(Date,Pos-1,2) SubStr(N,A_Index+1)
    Return, N ; digits >0 from Down in D stand for Date's values
}
Edits: YY to yy, Format:="yyyyMMddHHmmss"
Last edited by Rohwedder on 19 Apr 2021, 05:25, edited 2 times in total.
just me
Posts: 9442
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Calculate estimated time and end time

19 Apr 2021, 04:44

Albireo wrote: runTime.stoClock += runTime.totRunTime, Seconds

It works (but I thought it was complicated to use)
It doesn't:

Code: Select all

#NoEnv
RunTime := {}
RunTime.stoClock := SubStr(A_Now, 1, 8) . "000000"
Before := RunTime.stoClock
RunTime.stoClock += 999999, Seconds
After := RunTime.stoClock
MsgBox, Before:`t%Before%`nAfter:`t%After%

Code: Select all

---------------------------
N90376775.ahk
---------------------------
Before:	20210419000000
After:	20210419999999
---------------------------
OK
---------------------------
Rohwedder
Posts: 7610
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Calculate estimated time and end time

19 Apr 2021, 06:09

Now after my changes this works with my DateAdd():

Code: Select all

#NoEnv
RunTime := {}
RunTime.stoClock := SubStr(A_Now, 1, 8) . "000000"
Before := RunTime.stoClock
RunTime.stoClock := DateAdd(RunTime.stoClock, 999999, "Seconds")
After := RunTime.stoClock
MsgBox, Before:`t%Before%`nAfter:`t%After%
Before: 20210419000000
After: 20210430134639
Albireo
Posts: 1748
Joined: 16 Oct 2013, 13:53

Re: Calculate estimated time and end time

19 Apr 2021, 16:35

Thank you all! :dance:
The function() by @Rohwedder was easy to understand (and use)
Albireo
Posts: 1748
Joined: 16 Oct 2013, 13:53

Re: Calculate estimated time and end time

20 Apr 2021, 03:46

But it was not as easy as I thought ...

Now I know why the clock (eg. 12:05:47) doesn't show sometimes.
It was when the time count result was over 60 seconds (eg. 20210418111288)

Thought to use the function DateAdd() (from @Rohwedder ) and started from the beginning.

Before, I did this to get the start time as a clock (and it works)

Code: Select all

runTime["staClock"] := A_Now					; 20210418111211
FormatTime tmpClock, % runTime.staClock, HH:mm:ss	; 11:47:11
runTime["staClockShow"] := tmpClock				; the clock to an array 11:47:11
Then I tested the function like this .:

Code: Select all

runTime["staClockShow"] := DateAdd(A_Now, 0, "Seconds", "HH:mm:ss")
The result was 20:10:20 (insted of 10:43:23)
My testprogram .:
As result .: ---------------------------
20210420104323
20210420000000
20:10:20
10:43:23
20210420104323
---------------------------
Rohwedder
Posts: 7610
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Calculate estimated time and end time

20 Apr 2021, 04:11

You must give DateAdd() the correct Format. With
DateAdd(A_Now, 0, "Seconds", "HH:mm:ss")
you claim that A_Now has the Format "HH:mm:ss".
But, A_Now has "yyyyMMddHHmmss"!

Which Format has "01.02.03"?
US "yy.MM.dd", German "dd.MM.yy", time of day "HH.mm.ss" manufacturing hour "MM.dd.HH"?

Give DateAdd() input and output Format and DateDiff() two input Formats. But I like them as they are.
Albireo
Posts: 1748
Joined: 16 Oct 2013, 13:53

Re: Calculate estimated time and end time

20 Apr 2021, 09:41

ok!
I thought the input format was always yyyymmddhhmmss
And HH: mm: ss determined the format of the result :o
Albireo
Posts: 1748
Joined: 16 Oct 2013, 13:53

Re: Calculate estimated time and end time

24 Apr 2021, 17:41

Thank you!
Finaly - Got a result that I will use when the program run takes some time.

(Unfortunately, some text and comments are in Swedish)
Testprogram - Show Runtime
The test program works well, when the events take about the same amount of time.
Wondering about how the estimated - run time / end time could be improved.
The solution became much more complicated than I had thought from the beginning.
The possibility of being able to put the test program in pause / running mode has been inspired by this link .: toggle Pause - possible?
Rohwedder
Posts: 7610
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Calculate estimated time and end time

25 Apr 2021, 05:56

My functions simplify the handling of specially formatted times.
But since you do all your calculations in the standard format YYYYMMDDHH24MISS, they are superfluous.
The good old DateAdd() from jeeswg is enough for you.

Code: Select all

DateAdd(DateTime, Time, TimeUnits)
{ ;jeeswg: https://www.autohotkey.com/boards/viewtopic.php?f=37&t=29689 
    EnvAdd, DateTime, % Time, % TimeUnits
    return DateTime
}

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Mannaia666, ntepa and 245 guests