Jump to content
Lecdev

What is timerdiff() doing?

Recommended Posts

does anyone know what exactly TimerDiff() is doing with the integer returned by TimerInit() in order to get milliseconds?

 

Just trying to find out if i can use the integer timerinit() returns in a slightly different way to the norm because it will save me a great deal of effort.

 

( hoping to save the integer in a db and query rows where the difference in current time integer is > x where x = whatever the integer needs to be to = y hours )

 

 

Share this post


Link to post
Share on other sites

See by yourself there is only little difference in behavior of the two timer functions. They both basically deal with QueryPerformanceCounter.

Note that TimerInit returns a double and the built-in functions are unsurprisingly way faster (~20x)  than the UDF.

#include <Timers.au3>

Local $t1 = TimerInit()
Local $t2 = _Timer_Init()
ConsoleWrite($t1 & @TAB & VarGetType($t1) & @LF)
ConsoleWrite($t2 & @TAB & VarGetType($t2) & @LF)

Sleep(1000)

$t1 = TimerDiff($t1)
$t2 = _Timer_Diff($t2)
ConsoleWrite($t1 & @TAB & VarGetType($t1) & @LF)
ConsoleWrite($t2 & @TAB & VarGetType($t2) & @LF)

Local $total
For $i = 1 To 10000
    $t1 = TimerInit()
    $total += TimerDiff($t1)
Next
ConsoleWrite('Average TimerInit + TimerDiff invokation time = ' & $total & @LF)
$total = 0
For $i = 1 To 10000
    $t1 = _Timer_Init()
    $total += _Timer_Diff($t1)
Next
ConsoleWrite('Average _Timer_Init + _Timer_Diff invokation time = ' & $total & @LF)

To convert to usable precise relative (unsynchronized to UTC) time you'll have to use __Timer_QueryPerformanceCounter and  __Timer_QueryPerformanceFrequency (see those internal function in Timers.au3).

The int64 you'll get are perfect candidates for use in a DB but can't be used as primary key as the counter is reset at reboot. To get a UTC-synchronized high-res timestamp in 100ns increments from January 1, 1601 use GetSystemTimePreciseAsFileTime WinAPI call. Its result is guaranteed in sync with UTC and monotonically increasing in 100ns increments.


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

Just an FYI, from the help file.

Quote

Remarks

The return value from TimerInit() should be treated as an opaque handle and should only be used to pass to TimerDiff(). Any other usage of the return value is a potential error.

 


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

To expand on @jchd's post, TimerInit returns QueryPerformanceCounter. TimerDiff returns the difference of QueryPerformanceCounter (relative to QueryPerformanceCounter value captured by TimerInit), divided by QueryPerformanceFrequency, which will depend on CPU speed. If you call TimerDiff with no reference, it will give time since boot.

See this previous post of mine

 

Looking at this contrived example, the builtin TimerDiff and TimerInit functions are very performant, although you could calculate the QueryPerformanceCounter you want to see in the future ahead of time, to know if a certain amount of time has passed, and be more performant. Though these are incredibly contrived, as the loops don't do any useful work. 

 

#include <Timers.au3>

$dLoopLength=10000000

$hTimer=TimerInit()
For $i=1 to $dLoopLength
    $fDummytime=0
Next
$fDummytime=TimerDiff($hTimer)
ConsoleWrite("Empty For loop: "& $fDummytime&@CRLF)

$hTimer=TimerInit()
For $i=1 to $dLoopLength
    $fDummytime=TimerDiff($hTimer)
Next
ConsoleWrite("Builtin TimerDiff: "& $fDummytime&@CRLF)

$hTimer=TimerInit()
$fFreq= __Timer_QueryPerformanceFrequency()/1000
For $i=1 to $dLoopLength
    $fDummytime=(TimerInit()-$hTimer)/$fFreq
Next
ConsoleWrite("Hybrid TimerDiff: "& $fDummytime&@CRLF)

$dLoopLength=$dLoopLength/100

$hTimer=_Timer_Init()
For $i=1 to $dLoopLength
    $fDummytime=_Timer_Diff($hTimer)
Next
ConsoleWrite("UDF TimerDiff: "& $fDummytime*100&@CRLF)

$i=1
$hTimer=TimerInit()
Do
$i+=1
Until TimerDiff($hTimer) > 2000
ConsoleWrite("Builtin Timerdiff:"&$i&" loops"&@CRLF)

$i=1
$hTimer=TimerInit()
$fFreq= __Timer_QueryPerformanceFrequency()
$hTagetend=Int(TimerInit())+(2*$fFreq)
Do
$i+=1
Until TimerInit() > $hTagetend
ConsoleWrite("Hybrid Timerdiff:"&$i&" loops"&@CRLF)
Empty For loop: 1327.85372380903
Builtin TimerDiff: 3809.99572739426
Hybrid TimerDiff: 4339.62816893478
UDF TimerDiff: 151207.223724135
Builtin Timerdiff:3647645 loops
Hybrid Timerdiff:5246004 loops

 

Share this post


Link to post
Share on other sites
Quote

 7 years ago by Valik


If you use the return value from TimerInit() for anything other than an opaque handle to be passed to TimerDiff() then you are asking for trouble. Therefore the type is irrelevant. Ticket closed.

 

 


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

Fine. Then it's enough to DllCall the relevant (already-mentionned) Windows primitives which return an unambiguous Int64.


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Similar Content

    • By guinness
      This is for my C# fans out there who enjoy OOP. I have created a really rough draft (I mean it's super basic) of the Stopwatch Class in .NET, because I had the idea whilst travelling home and just thought it's neat to show a different paradigm to that of procedural programming.
       
      Enjoy.

      #include <WinAPISys.au3> Global Const $STOPWATCH_GUID = '804835D8-0DF4-11E4-A11F-29560707A45E', $STOPWATCH_TICKSPERMILLISECOND = 10000, $STOPWATCH_TICKSPERSECOND = $STOPWATCH_TICKSPERMILLISECOND * 1000 Global Enum $STOPWATCH_TIMER, $STOPWATCH_RUNNING, $STOPWATCH_ELAPSED, $STOPWATCH_ISHIGHRESOLUTION, $STOPWATCH_FREQUENCY, $STOPWATCH_TICKFREQUENCY, $STOPWATCH_ID, $STOPWATCH_MAX #Region Example Example() Func Example() Local $hStopWatch = Stopwatch() ConsoleWrite('IsRunning: ' & Stopwatch_IsRunning($hStopWatch) & @CRLF) ; Display running status. ConsoleWrite('Started' & @CRLF) Stopwatch_Start($hStopWatch) ; Start the stopwatch. ConsoleWrite('IsRunning: ' & Stopwatch_IsRunning($hStopWatch) & @CRLF) ; Display running status. Sleep(1000) ; Wait for 1 second. Stopwatch_Stop($hStopWatch) ; Stop the stopwatch. ConsoleWrite('Elapsed ms Time: ' & Stopwatch_ElapsedMilliseconds($hStopWatch) & @CRLF) ConsoleWrite('Stopped.' & @CRLF) ConsoleWrite('Waiting for 3 seconds whilst the stopwatch is stopped.' & @CRLF) Sleep(3000) ; Wait for 3 seconds. ConsoleWrite('Started' & @CRLF) Stopwatch_Start($hStopWatch) ; Start the stopwatch again. ConsoleWrite('Waiting for 2 seconds whilst the stopwatch is running' & @CRLF) Sleep(2000) ; Wait for 2 seconds. For $i = 1 To 10 ; The number of milliseconds is shown even when the stopwatch is running. ConsoleWrite('Elapsed ms Time: ' & Stopwatch_ElapsedMilliseconds($hStopWatch) & @CRLF) Sleep(250) Next Stopwatch_Stop($hStopWatch) ; Stop the stopwatch. ConsoleWrite('Elapsed ms Time: ' & Stopwatch_ElapsedMilliseconds($hStopWatch) & @CRLF) ; This should be about 5 seconds or so. ConsoleWrite('Stopped' & @CRLF) ConsoleWrite('IsRunning: ' & Stopwatch_IsRunning($hStopWatch) & @CRLF) ; Display running status. EndFunc ;==>Example #EndRegion Example ; Stopwatch Class: http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch(v=vs.110).aspx Func Stopwatch() Local $aStopwatch[$STOPWATCH_MAX] $aStopwatch[$STOPWATCH_ID] = $STOPWATCH_GUID Stopwatch_Reset($aStopwatch) $aStopwatch[$STOPWATCH_FREQUENCY] = _WinAPI_QueryPerformanceFrequency() If $aStopwatch[$STOPWATCH_FREQUENCY] > 0 Then $aStopwatch[$STOPWATCH_ISHIGHRESOLUTION] = True $aStopwatch[$STOPWATCH_TICKFREQUENCY] = $STOPWATCH_TICKSPERSECOND $aStopwatch[$STOPWATCH_TICKFREQUENCY] /= $aStopwatch[$STOPWATCH_FREQUENCY] Else $aStopwatch[$STOPWATCH_ISHIGHRESOLUTION] = False $aStopwatch[$STOPWATCH_FREQUENCY] = $STOPWATCH_TICKSPERSECOND $aStopwatch[$STOPWATCH_TICKFREQUENCY] = 1 EndIf Return $aStopwatch EndFunc ;==>Stopwatch Func Stopwatch_ElapsedMilliseconds(ByRef $aStopwatch) Return (__Stopwatch_IsStopwatch($aStopwatch) ? __Stopwatch_GetElapsedDateTimeTicks($aStopwatch) / $STOPWATCH_TICKSPERMILLISECOND : 0) EndFunc ;==>Stopwatch_ElapsedMilliseconds Func Stopwatch_ElapsedTicks(ByRef $aStopwatch) Return (__Stopwatch_IsStopwatch($aStopwatch) ? __Stopwatch_GetElapsedDateTimeTicks($aStopwatch) : 0) EndFunc ;==>Stopwatch_ElapsedTicks Func Stopwatch_IsRunning(ByRef $aStopwatch) Return (__Stopwatch_IsStopwatch($aStopwatch) ? $aStopwatch[$STOPWATCH_RUNNING] : Null) EndFunc ;==>Stopwatch_IsRunning Func Stopwatch_Reset(ByRef $aStopwatch) If __Stopwatch_IsStopwatch($aStopwatch) Then $aStopwatch[$STOPWATCH_ELAPSED] = 0 $aStopwatch[$STOPWATCH_RUNNING] = False $aStopwatch[$STOPWATCH_TIMER] = 0 EndIf Return True EndFunc ;==>Stopwatch_Reset Func Stopwatch_Restart(ByRef $aStopwatch) If __Stopwatch_IsStopwatch($aStopwatch) Then Stopwatch_Reset($aStopwatch) Stopwatch_Start($aStopwatch) EndIf Return True EndFunc ;==>Stopwatch_Restart Func Stopwatch_Start(ByRef $aStopwatch) If __Stopwatch_IsStopwatch($aStopwatch) And Not $aStopwatch[$STOPWATCH_RUNNING] Then $aStopwatch[$STOPWATCH_RUNNING] = True $aStopwatch[$STOPWATCH_TIMER] = __Stopwatch_GetTimestamp($aStopwatch) EndIf Return True EndFunc ;==>Stopwatch_Start Func Stopwatch_StartNew() Local $aStopwatch = Stopwatch() Stopwatch_Start($aStopwatch) Return $aStopwatch EndFunc ;==>Stopwatch_StartNew Func Stopwatch_Stop(ByRef $aStopwatch) If __Stopwatch_IsStopwatch($aStopwatch) And $aStopwatch[$STOPWATCH_RUNNING] Then $aStopwatch[$STOPWATCH_ELAPSED] = __Stopwatch_GetRawElapsedTicks($aStopwatch) $aStopwatch[$STOPWATCH_RUNNING] = False EndIf Return True EndFunc ;==>Stopwatch_Stop Func __Stopwatch_GetElapsedDateTimeTicks(ByRef $aStopwatch) Return (__Stopwatch_GetRawElapsedTicks($aStopwatch) * ($aStopwatch[$STOPWATCH_ISHIGHRESOLUTION] ? $aStopwatch[$STOPWATCH_TICKFREQUENCY] : $STOPWATCH_TICKSPERMILLISECOND)) EndFunc ;==>__Stopwatch_GetElapsedDateTimeTicks Func __Stopwatch_GetRawElapsedTicks(ByRef $aStopwatch) Local $iElapsedTime = $aStopwatch[$STOPWATCH_ELAPSED] If $aStopwatch[$STOPWATCH_RUNNING] Then If $aStopwatch[$STOPWATCH_ISHIGHRESOLUTION] Then Local $iTimeStamp = __Stopwatch_GetTimestamp($aStopwatch) $iTimeStamp = $iTimeStamp - $aStopwatch[$STOPWATCH_TIMER] $iElapsedTime += $iTimeStamp Else $iElapsedTime += TimerDiff($aStopwatch[$STOPWATCH_TIMER]) EndIf EndIf Return $iElapsedTime EndFunc ;==>__Stopwatch_GetRawElapsedTicks Func __Stopwatch_GetTimestamp(ByRef $aStopwatch) Return ($aStopwatch[$STOPWATCH_ISHIGHRESOLUTION] ? _WinAPI_QueryPerformanceCounter() : TimerInit()) EndFunc ;==>__Stopwatch_GetTimestamp Func __Stopwatch_IsStopwatch(ByRef $aStopwatch) ; Internal function only. Return (UBound($aStopwatch) = $STOPWATCH_MAX And $aStopwatch[$STOPWATCH_ID] = $STOPWATCH_GUID) EndFunc ;==>__Stopwatch_IsStopwatch
    • By Anne
      Hi everyone.
      I have a question and wasn't able to find any answer for this.
      What is the maximum value/limit for TimerInit & TimerDiff.
      I mean, If I use TimerInit() in my script, how long can it 'last'? How long will I be able to read it with TimerDiff()?
      I realize that the result of TimerDiff isn't an Integer. It's a floating number and I am not sure what the maximum value is.
      I would like my program to use $Time = TimerInit() today, and then I'll be able to use TimerDiff($Time) tomorrow or even the day after. Is this possible? and what happens if TimerDiff exceeds the maximum value (if there is any)?
    • By n0pr0bl3m
      So I have some timers which I reset periodically, but my problem is that they don't wait for the functions before them to finish and in a strange way reset beforehand.

      -snip-

      timerinit doesnt wait the mouseclick and sleep functions to finish
×
×
  • Create New...