Code Timer Macros

Post your FreeBASIC tips and tricks here. Please don’t post your code without including an explanation.
MichaelW
Posts: 3500
Joined: May 16, 2006 22:34
Location: USA

Code Timer Macros

Postby MichaelW » May 22, 2006 22:51

Code: Select all

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' The TIMER_BEGIN and TIMER_END macros, along with the support code and
' data, provide a convenient method of measuring the execution time for
' a block of code. These macros must be called in pairs, and the block
' of code must be placed in between the TIMER_BEGIN and TIMER_END macro
' calls. The execution time is averaged over the specified number of
' loops, with higher loop counts producing better accuracy and
' repeatability. The execution time in milliseconds is returned in the
' shared variable TIMER_MS.
'
' To reduce the frequency of context switches in the test loop the
' TIMER_BEGIN macro sets the priority class for the current process to
' HIGH_PRIORITY_CLASS. After the loop has completed, the TIMER_END macro
' (arbitrarily) sets the priority class to NORMAL_PRIORITY_CLASS.
'
' These macros use the high-resolution performance counter. The return
' value will be zero if the high-resolution performance counter is not
' available.
'
' To support future debugging and/or modifications, all of the variables
' are shared. The odd names minimize the possibility of name conflicts.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

dim shared as ulongint _timer_pc_frequency_, _timer_pc_count_total_
dim shared as ulongint _timer_pc_count1_, _timer_pc_count2_
dim shared as double _timer_temp_
dim shared as uinteger _timer_loop_counter_, _timer_loop_count_, timer_ms

#define TIMER_BEGIN( loopcount ) _
    _timer_pc_count_total_ = 0 :_
    _timer_loop_count_ = loopcount :_
    if QueryPerformanceFrequency( cast(PLARGE_INTEGER, @_timer_pc_frequency_) ) then :_
        SetPriorityClass( GetCurrentProcess(), HIGH_PRIORITY_CLASS ) :_
        for _timer_loop_counter_ = 1 to _timer_loop_count_ :_       
            QueryPerformanceCounter( cast(PLARGE_INTEGER, @_timer_pc_count1_) ) :_
'           
' *** Note the open FOR loop and IF block ***
'
#define TIMER_END _
            QueryPerformanceCounter( cast(PLARGE_INTEGER, @_timer_pc_count2_) ) :_
            _timer_pc_count_total_ += _timer_pc_count2_ - _timer_pc_count1_ :_
        next :_
        SetPriorityClass( GetCurrentProcess(), NORMAL_PRIORITY_CLASS ) :_
        _timer_temp_ = _timer_pc_count_total_ / _timer_loop_count_ :_
        _timer_temp_ *= 1000 :_
        _timer_temp_ /= _timer_pc_frequency_ :_
        timer_ms = _timer_temp_ :_
    else :_
        timer_ms = 0 :_
    endif

Return to “Tips and Tricks”

Who is online

Users browsing this forum: No registered users and 6 guests