# How to Calculate Time Difference in Milliseconds

## Recommended Posts

\$Previous_Time = TimerInit ()

Sleep (5000) ;sleep five seconds

\$Current_Time = \$TimerInit ()

\$Time_Difference = \$Current_Time - \$Previous_Time

Is the time difference calculation proper? I expect \$Time_Difference to contain the millisecond time difference. It doesn't appear to.

I know about the TimerDiff function, but in my case I want to get the millisecond time difference in two times that are stored. Both times were obtained via \$TimerInit.

Paul

##### Share on other sites

Like this?

```#include <GuiConstants.au3>
#include <_Talk.au3>
GuiCreate("Timer", 300, 118,-1, -1 , BitOR(\$WS_OVERLAPPEDWINDOW, \$WS_CLIPSIBLINGS))

\$Label_5 = GuiCtrlCreateLabel("Enter the time and press begin, a msgbox will prompt when the time has finished.", 20, 0, 240, 30)
\$Hours = GuiCtrlCreateInput("Hours", 10, 90, 50, 20)
\$Mins = GuiCtrlCreateInput("Minutes", 70, 90, 50, 20)
\$Sec = GuiCtrlCreateInput("Seconds", 130, 90, 50, 20)
\$Button_4 = GuiCtrlCreateButton("Begin", 210, 90, 80, 20)
GuiSetState()
While 1
\$msg = GuiGetMsg()
Select
Case \$msg = \$Button_4
Sleep(\$hms + \$mms + \$sms)
_Talk("rawr the time is up")
MsgBox(0, "", "Time's up!" & @CRLF & "Elapsed Time: " & \$h & " : " & \$m & " : " & \$s)
Case \$msg = \$GUI_EVENT_CLOSE
ExitLoop
Case Else
;;;
EndSelect
WEnd
Exit```
Edited by gamepin126

##### Share on other sites

I believe this is what you want:

```\$Previous_Time = TimerInit()
Sleep (5000) ;sleep five seconds
\$Time_Difference = TimerDiff(\$Previous_Time)
MsgBox(0,"Diff",\$Time_Difference)```

Don't argue with an idiot; people watching may not be able to tell the difference.

##### Share on other sites

I know about the TimerDiff function, but in my case I want to get the millisecond time difference in two times that are stored. Both times were obtained via \$TimerInit.

actually TimerInit does not really return a time stamp in milliseconds, but rather an "internal" number. If you look at the TimerDiff() source code, you'll see, that the frequency of the high-resolution performance counter is used in the calculation of the time difference in milliseconds.

```script_misc.cpp: F_TimerDiff

__int64 freq, now;

if (!QueryPerformanceFrequency((LARGE_INTEGER *)&freq))
return AUT_OK;

if (!QueryPerformanceCounter((LARGE_INTEGER *)&now))
return AUT_OK;

vResult = (((double)now - vParams[0].fValue()) / (double)freq) * 1000.0;```

So, you can't simply subtract the two values you retrieved via TimerInit(). I guess, you'll have to use

TimerDiff().

EDIT: However, TimerDiff() could be modified to accept a second argument. Like this: \$diff = TimerDiff(\$start,\$end), so it would not take the current value of the performance counter (now), but rather the specified values of \$start and \$end". Code could look like this:

```script_misc.cpp: F_TimerDiff

__int64 freq, now;

if (!QueryPerformanceFrequency((LARGE_INTEGER *)&freq))
return AUT_OK;

if (!QueryPerformanceCounter((LARGE_INTEGER *)&now))
return AUT_OK;

if (vParams.size() == 2)
vResult = ((vParams[1].fValue() - vParams[0].fValue()) / (double)freq) * 1000.0;
else
vResult = (((double)now - vParams[0].fValue()) / (double)freq) * 1000.0;

return AUT_OK;```

Cheers

Kurt

Edited by /dev/null

__________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *

##### Share on other sites

Thanks for the replies.

Kurt, you're right -- I need to calculate the time difference, in milliseconds, between two stored times. TimerDiff doesn't help in this case, but the modifed TimerDiff you posted certainly would. The problem is: I don't understand. I am using Autoit V3. I don't see where the TimerDiff function is shipped with the standard download.

Can I use what you posted and how would I use it? My Autoit application is just a batch script, not a GUI.

Thank you VERY MUCH.

Paul

##### Share on other sites

Thanks for the replies.

Kurt, you're right -- I need to calculate the time difference, in milliseconds, between two stored times. TimerDiff doesn't help in this case, but the modifed TimerDiff you posted certainly would. The problem is: I don't understand. I am using Autoit V3. I don't see where the TimerDiff function is shipped with the standard download.

Can I use what you posted and how would I use it? My Autoit application is just a batch script, not a GUI.

Thank you VERY MUCH.

Paul

You'll find it quite useful if you want to run most of the scripts and have all the functionality that people talk about to download the Beta of AutoIt.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

##### Share on other sites

Can I use what you posted and how would I use it? My Autoit application is just a batch script, not a GUI.

Paul,

I just proposed a possible change to the current functionality of TimerDiff(). It is not yet implemented nor can you download it somewhere. I just used the available source code of AutoIT 3 and made a very small change to fit your requirements. I would have to submit those changes to the DEV team and wait for an inclusion in the next beta release. However, it's totally unclear if my change request will be accepted. So, for the moment there is unfortunately nothing you can do.

Maybe you want to describe why you need the difference of two timestamps? I guess the forum members can then propose another solution.

EDIT: well there is one thing you could do. Do a DllCall on QueryPerformanceFrequency and QueryPerformanceCounter and "simulate" the internals of TimerInit() and TimerDiff().

Cheers

Kurt

Edited by /dev/null

__________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *

##### Share on other sites

...I don't see where the TimerDiff function is shipped with the standard download...

...but it is:

http://www.autoitscript.com/autoit3/docs/f...s/TimerDiff.htm

:-)

[size="1"][font="Arial"].[u].[/u][/font][/size]

##### Share on other sites

Cheers

Kurt

__________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *

##### Share on other sites

maybe this... from autoit wrappers

```; timer to "thousandths" of a second
; Author - Holger

While 1
ToolTip(@Hour & ':' & @Min & ':' & @Sec & ':' & _MSec())
Sleep(1)
WEnd

Exit

Func _MSec()
Local \$stSystemTime = DllStructCreate('ushort;ushort;ushort;ushort;ushort;ushort;ushort;ushort')
DllCall('kernel32.dll', 'none', 'GetSystemTime', 'ptr', DllStructGetPtr(\$stSystemTime))

\$sMilliSeconds = StringFormat('%03d', DllStructGetData(\$stSystemTime, 8))

\$stSystemTime = 0

Return \$sMilliSeconds
EndFunc```

8)

##### Share on other sites

Cheers

Kurt

My brief post was meant to convey that "The TimerDiff function is shipped with the standard download."

Upon review, it was probably not worth posting. Sorry for any confusion.

I hope that you proposed version makes it in...

@Valuater - Nice find.

@Holger - Must be nice to just build you own :-)

[size="1"][font="Arial"].[u].[/u][/font][/size]

## Create an account

Register a new account

• ### Recently Browsing   0 members

×

• Wiki

• Back

• #### Beta

• Git
• FAQ
• Our Picks
×
• Create New...