简体   繁体   中英

Measuring time for the execution of functions

I am stuck at measuring execution time for the functions in C++. I tried clock_t clock(void) but it measures just the time for the one function in the program. In the below simple example how can i measure time for the two functions?

#include <iostream>

using namespace std;

int add(int a, int b)
{
    return a+b;
}

int subtract(int a, int b)
{
    return a - b;
}

int main()
{
    int a,b;
    cin >> a >> b;

    int c = add(a,b);
    int d = subtract(a,b);
    cout << c << " " << d << "\n";
    return 0;
}

If you want to do benchmarking, instead of using std::clock_t you should consider using time_point<high_resolution_clock> using std::chrono :

    using namespace std::chrono; 

    high_resolution_clock::time_point t = high_resolution_clock::now();
    //... do what is to be measured
    high_resolution_clock::time_point t2 = high_resolution_clock::now();
    cout << duration_cast<milliseconds>(t2 - t).count();

However, timing is always an imprecise measure that depends on the clock resolution (on windows for example, the resolution is around 15 ms). So the shorter the time you measure, the higher is the relative error in the measure. So if your function takes 15 ms to execute, on windows, you'll have an error in the measure of +/- 100%.

The only way you can increase reliability of the measure, is to measure a longer time, by having a huge number of iterations. So in the example, with 1 million iterations, the relative error of the measure is 0,0001%.

If you can benchmark your functions separately, just do it like that. But if you can't measure your functions separately (for example because one produces the input for the other), you can't use this approach). If you want to find the bottleneck your code in such case, then you should use the right tool: a profiler .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM