Audio latency measuring sample for Windows

This code sample demonstrates how to measure minimum, maximum and average execution times of a routine or a portion of code with high resolution time stamps in Visual C++. The values obtained are calculated in microseconds.

// Code to measure minimum, maximum and average execution times of a routine in microseconds
// NOTE: build a RELEASE version of this code to get useful results

// The data that we are interested in:
float minExecutionTime,maxExecutionTime,averageExecutionTime;

// Other vars
float currentExecutionTime,totalExecutionTime,freqFactor;
ULONG executionCount;
BOOL doMeasure;

void Initialization(void) ;
void UpdateStats(LONGLONG elapsed);

// Run this code only once at initialization
void Initialization(void)
    LARGE_INTEGER perfFrequency;


    // Get performance frequency in counts per second
    if (QueryPerformanceFrequency(&perfFrequency)!=TRUE)
        // Something went wrong, measuring will not work
        // The freqFactor is the amount of microseconds of a single performance count


void UpdateStats(LONGLONG elapsed)
    // Calculated elapsed execution time in microseconds

    // Update minimum execution time
    if (currentExecutionTime < minExecutionTime || minExecutionTime==0)

    // Update maximum execution time
    if (currentExecutionTime > maxExecutionTime)

    // increase number of successful measurings

    // Update total and average


// Your code to be measured, this could be your audio process function
// Make sure that Initialization() has been called once before
void MyCodeToBeMeasured(void)
    LARGE_INTEGER start={0,0},end={0,0};

    if (doMeasure==TRUE)
        // Set this code to run only at first CPU
        // If we are running on another CPU then we get rescheduled immediately
        SetThreadAffinityMask (GetCurrentThread(),1);

        // Get the performance time stamp at the beginning of your code

    // ...
    // now run the code you wish to be measured
    // ...

    // Get the performance time stamp at the end of your code
    // and update our data
    if (doMeasure==TRUE)

        if (start.QuadPart > 0 && end.QuadPart > 0)

        // minExecutionTime, maxExecutionTime and averageExecutionTime are now filled
        // with useful data representing execution times in microseconds