High Resolution Timing Part of Your Code
Asked Answered
I

6

7

I want to measure the speed of a function within a loop. But why my way of doing it always print "0" instead of high-res timing with 9 digits decimal precision (i.e. in nano/micro seconds)?

What's the correct way to do it?

#include <iomanip>
#include <iostream>
#include <time.h>
int main() {


 for (int i = 0; i <100; i++) {
    std::clock_t startTime = std::clock(); 
    // a very fast function in the middle
    cout << "Time: " << setprecision(9) << (clock() - startTime + 0.00)/CLOCKS_PER_SEC << endl;
 }

 return 0;
}

Related Questions:

Inalienable answered 31/3, 2009 at 7:22 Comment(1)
+1 Just the link I was looking for!Bluenose
H
6

Move your time calculation functions outside for () { .. } statement then devide total execution time by the number of operations in your testing loop.

#include <iostream>
#include <ctime>
#define NUMBER 10000 // the number of operations

// get the difference between start and end time and devide by
// the number of operations
double diffclock(clock_t clock1, clock_t clock2)
{
    double diffticks = clock1 - clock2;
    double diffms = (diffticks) / (CLOCKS_PER_SEC / NUMBER);
    return diffms;
}

int main() {
    // start a timer here
    clock_t begin = clock();

    // execute your functions several times (at least 10'000)
    for (int i = 0; i < NUMBER; i++) {
        // a very fast function in the middle
        func()
    }

    // stop timer here
    clock_t end = clock();

    // display results here
    cout << "Execution time: " << diffclock(end, begin) << " ms." << endl;
    return 0;
}

Note: std::clock() lacks sufficient precision for profiling. Reference.

Henotheism answered 31/3, 2009 at 7:25 Comment(1)
you need to divide by 100 to get the time per loopPearlstein
E
3

A few pointers:

  1. I would be careful with the optimizer, it might throw all your code if I will think that it doesn't do anything.
  2. You might want to run the loop 100000 times.
  3. Before doing the total time calc store the current time in a variable.
  4. Run your program several times.
Equuleus answered 31/3, 2009 at 7:42 Comment(0)
H
3

If you need higher resolution, the only way to go is platform dependent.

On Windows, check out the QueryPerformanceCounter/QueryPerformanceFrequency API's.

On Linux, look up clock_gettime().

Hydrated answered 31/3, 2009 at 7:46 Comment(0)
H
2

See a question I asked about the same thing: apparently clock()'s resolution is not guaranteed to be so high.

C++ obtaining milliseconds time on Linux -- clock() doesn't seem to work properly

Try gettimeofday function, or boost

Houseboy answered 31/3, 2009 at 7:46 Comment(0)
G
2

If you need platform independence you need to use something like ACE_High_Res_Timer (http://www.dre.vanderbilt.edu/Doxygen/5.6.8/html/ace/a00244.html)

Glioma answered 6/4, 2009 at 3:17 Comment(0)
S
1

You might want to look into using openMp.

#include <omp.h>

int main(int argc, char* argv[])
{       
    double start = omp_get_wtime();

    // code to be checked

    double end = omp_get_wtime();

    double result = end - start;

    return 0;
}
Side answered 31/3, 2009 at 7:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.