A Coder
A Coder

Reputation: 27

Implementing Timer to Measure Execution Time of Function Passed as Parameter

For ordinary functions, at least those of a single parameter, there is no problem. However, I am trying to measure the execution time for a bunch of different sorting algorithms that have multiple parameters, one of which is a vector.

Here is the timer contained in main:

clock_t begin = clock();
FunctionToTime();
clock_t end = clock();
double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;
printf ("Elapsed time is %.2lf seconds.", elapsed_secs );
return 0;

Example of sample function to be passed as parameter to timer: SelectionSort(dataVector, dataVector.size());

Is it possible to make a function that accepts the sorting algorithm as a function parameter and measures the execution time of that inner function?

I have tried a few approaches, but none of them have been successful. Is this possible?

Upvotes: 0

Views: 381

Answers (2)

Charlie
Charlie

Reputation: 653

#include <windows.h> //win header for large integer and query performance

consider this stopwatch class:

class stopwatch
{
double PCFreq = 0.0;
__int64 CounterStart = 0;
LARGE_INTEGER li;

public:
    void StartCounter()
    {
        if (!QueryPerformanceFrequency(&li))ExitProcess(0);
        PCFreq = double(li.QuadPart) / 1000.0;
        QueryPerformanceCounter(&li); CounterStart = li.QuadPart;
    }

    double GetCounter() 
    { 
        QueryPerformanceCounter(&li); 
        return double(li.QuadPart - CounterStart) / PCFreq; 
    }
};

To make it work, you need to create an object of the class, say:

stopwatch mystopwatch;

that will have the functions to start/reset the timer and check it:

mystopwatch.StartCounter();//starts this specific objects timer over
mystopwatch.GetCounter(); //returns value in milliseconds

This way, you can tell it to start running, and query it again asking how much time has passed. (its very precise)

while (mystopwatch.clockflag <= 5){};  //waits 5 milliseconds

Using this to solve your problem wouldnt be very hard. Something like:

double SelectionSort(dataVector, dataVector.size())
{
    stopwatch mystopwatch;
    mystopwatch.StartCounter(); //starts the clock timer
//...run actual function here..//
    return mystopwatch.GetCounter(); //return time in milliseconds
}

will do the calculations, start the timer, then return the time it took as a double in milliseconds.

Upvotes: 1

SergeyA
SergeyA

Reputation: 62613

Your question is not about timing functions, it is about generic function wrappers. With C++14, it is a trivial task.

Following is an example:

template <class F, class... ARGS>
auto wrapper(F func, ARGS&&... args) {
    clock_t begin = clock(); // better stuff from chrono
    auto r = func(std::forward<ARGS...>(args...));
    clock_t end = clock();
    double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;
    printf ("Elapsed time is %.2lf seconds.", elapsed_secs );

    return r;
}

Upvotes: 0

Related Questions