Timer for the C++

I am doing tower of hanoi, and need calculate the time for the function. However,i try many type such as time.h or windows.h...Since the function is recursive and very fast to end. hence,the result always be 0 since time start -time end equal 0.So, I am here to ask all of the experts how i can get more precise time value...thanks alot..
Since your in windows, the best-performance timing you can get is with QueryPerformanceCounter();

I don't have Windows here so I can't test it, but this is probably what your code would look like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <windows.h>
#include <iostream>

int main()
{
    LARGE_INTEGER frequency;
    LARGE_INTEGER start;
    LARGE_INTEGER stop;

    QueryPerformanceFrequency(&frequency);
    QueryPerformanceCounter(&start);

    YourFunctionHere();

    QueryPerformanceCounter(&stop);

    double timespent = (double)(stop.QuadPart - start.QuadPart) / (double)frequency.QuadPart;

    std::cout << timespent;

    return 0;
};


If you still have problems timing your function, then try running it many times over in a loop. That'll give you a larger number, then you can divide the time taken by the number of iterations to see how well it performed.
Since this is C++, you could probably clean up your main function as well:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <windows.h>
class HighResTimer
{
    LARGE_INTEGER start;
    LARGE_INTEGER stop;
    double frequency;
public:
    HighResTimer()
    {
        LARGE_INTEGER freq;
        QueryPerformanceFrequency(&freq);
        frequency = (double)freq.QuadPart;
    }
    void StartTimer()
    {
        QueryPerformanceCounter(&start);
    }
    double StopTimer()
    {
        QueryPerformanceCounter(&stop);
        return ((stop.QuadPart - start.QuadPart)/frequency);
    }
};


int main()
{
    HighResTimer timer;
    timer.StartTimer();
    
    YourFunctionHere();
    
    std::cout << timer.StopTimer();
}
> need calculate the time for the function. However,i try many type such as time.h or windows.h...
> Since the function is recursive and very fast to end. hence,the result always be 0.

Even if the result is not zero, a single measurement would tend to be somewhat inaccurate because the clock ticks at discrete intervals. The solution to both problems is: measure the approximate total time taken for a number of executions of the function (say a thousand times) and then compute the average time taken for a single execution.

For instance:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <ctime>
#include <cstdlib>
#include <iostream>

int fn()
{
    int r = 0 ;
    for( int i = 0 ; i < 1000 ; ++i ) r += std::rand() % 2 ;
    return r ;
}

int main()
{
    // get an estimate of the processor time used
    auto start = std::clock() ;

    // operation to time
    static volatile int v = 0 ;
    constexpr int N = 1000 ;
    for( int i = 0 ; i < N ; ++i ) v += fn() ;

    const double elapsed = std::clock() - start ;
    std::cout << "approximate processor time for a single call of fn: "
               << (elapsed*1000000) / (CLOCKS_PER_SEC*N)
               << " microseconds\n" ;
}

http://ideone.com/F0nU52
Topic archived. No new replies allowed.