Jump to content

What is timerdiff() doing?


 Share

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 )

 

 

Link to comment
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)

Link to comment
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

Link to comment
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

 

Link to comment
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

Link to comment
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)

Link to comment
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
 Share

×
×
  • Create New...