GetPerformanceCounter
- retrieves the current value of the high-resolution performance counter

Miscellaneous functions
(AFL 2.90)


SYNTAX GetPerformanceCounter( bReset = False )
RETURNS NUMBER
FUNCTION GetPerformanceCounter retrieves the current value of the high-resolution performance counter. Returned value is in milliseconds. Resolution is upto 0.001 ms (1 microsecond). The value of high-resolution counter represents number of milliseconds from either system start (boot) or from last counter reset. To reset the counter you need to call GetPerformanceCounter function with bReset parameter set to True.

Note that reseting counters inside one formula does not affect counters in other formulas. Since returned values are very large (time in milliseconds since system start is usually quite large), for precise measurements of single function or small function block execution times it is strongly recommended to reset counter at the beginning of the block so floating point resolution (7 digits) does not affect the precision of measurement.

GetPerformanceCounter function can be also used in trading system automation to measure time in milliseconds between various events (just subtract values returned by GetPerformanceCounter() during two different events)

Caveat: this function relies on Windows API QueryPerformanceCounter function and CPU RTDSC instruction and it may yield to inaccurrate results if you have multiple-core processor and AMD's "Cool and Quiet" enabled in BIOS or other CPU clock stepping technologies enabled. If this applies to you, you may check Microsoft hotfix to this problem at: http://support.microsoft.com/?id=896256

EXAMPLE ////////////////////////////////
// EXAMPLE 1
// The code shows that 1000 iterations of sin() calculation
// takes about 1.7 milliseconds.
// Note that call to the GetPerformanceCounter()
// has overhead of about 0.015 ms (15 microseconds)

GetPerformanceCounter(True); // reset counter to zero
for( i = 0; i < 1000; i++ )
{
   k =
sin( i );
}

elapsed=
GetPerformanceCounter();

"Time [ms] = "+elapsed;




////////////////////////////////
// EXAMPLE 2
// GetPerformanceCounter function
// may also be used to report time since system start.

elapsed=
GetPerformanceCounter();

StrFormat("Time since system start %.0f days, %.0f hours, %.0f minutes, %.0f seconds, %.0f milliseconds ",
floor(elapsed/(24*60*60*1000)),
floor( elapsed/(60*60*1000) ) % 24,
floor( elapsed/(60*1000) ) % 60,
floor( elapsed/1000 ) % 60,
elapsed %
1000 );
SEE ALSO

References:

The GetPerformanceCounter function is used in the following formulas in AFL on-line library:

More information:

Updated on-line reference