-1

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;
}
StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458
Emil
  • 73
  • 1
  • 12
  • 1
    Please read [Why you should not `#include `](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h). Coupling that with [`using namespace std;`](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) is even worse. – StoryTeller - Unslander Monica Nov 11 '18 at 09:18
  • Show what you have tried so far. – Swordfish Nov 11 '18 at 09:22
  • See here: [Easily measure elapsed time using ``](https://stackoverflow.com/questions/2808398/easily-measure-elapsed-time/21995693#21995693)... – Ruks Nov 11 '18 at 09:45
  • 2
    Welcome to Stack Overflow! Please take the [tour] and read [ask]. Concerning your question, please also do at least a little bit of research. Just searching the web for something like "how to measure time in C++" should turn up hundreds of hit and at least some of them should do the job for you. Also, as mentioned, your problem description is vague. Your question could be paraphrased like "here's some code, I tried to modify it in some way I only hint at but it didn't fulfil my unclear expectations". That's unfortunately a bad question. – Ulrich Eckhardt Nov 11 '18 at 09:46
  • 2
    Generally speaking, there is little point in trying to measure the time for a single function call that does a trivial operation like integer addition. There is too much variation in the times to make such measurement meaningful - do it a number of times, and the measurements will change. Instead, call the function some large number of times, to get a statistical measure (e.g. mean). Generally it will be necessary to obtain time before running the functions, obtain time after the functions are complete, and compute the difference. – Peter Nov 11 '18 at 09:47

1 Answers1

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.

Christophe
  • 68,716
  • 7
  • 72
  • 138