0

I have one function to profile for time. Actually the function shows time of 13222668 microseconds (13 seconds) when checked in main function. Then I started to profile different parts of function, minimal example is shown below:

#include <iostream>
#include <sstream>
#include <string>
#include <chrono>
using namespace std::chrono;
unsigned int match_time;
int func(){ 
  unsigned int i = 0;
  while(i < 20){
   high_resolution_clock::time_point t1 = high_resolution_clock::now(); // if I start the time from here, it shows me just `827687` milliseconds (827 milliseconds). 
   //code....
    {
        //code...

        {
           //code....
        }
          //code....
    }
        high_resolution_clock::time_point t2 = high_resolution_clock::now();
        auto duration = duration_cast<microseconds>( t2 - t1 ).count();
        match_time += duration;
        i++;
    }
    return 0;
   }

If I start the time within the while loop , it shows just 827687 microseconds (827 milliseconds), but If I start above the while loop

 high_resolution_clock::time_point t1 = high_resolution_clock::now();
 while(i < 20){
}
high_resolution_clock::time_point t2 = high_resolution_clock::now();
auto duration = duration_cast<microseconds>( t2 - t1 ).count();
match_time += duration;

I got a high time 13222668 microseconds (13 seconds), which I doubt about.

Also when I checked the time for different statement execution, I get 0 time when printing (which I think is a printing problem) and the summation match_time += duration; of which shows (827 milliseconds) during millions of runs.

I would like to know if my time profiling way is correct, and why the time varies within the while loop and outside loop (about outside loop time I doubt). Is my code hanging for some time due to any other reason.

Agaz Wani
  • 5,514
  • 8
  • 42
  • 62
  • [this](http://stackoverflow.com/q/42457605/841108) is a related question. – Basile Starynkevitch Feb 25 '17 at 16:26
  • @BasileStarynkevitch Thanks, but I need to know why the time varies, I think there is some issue that holds my code for a while which is clearly shown if I use the time outside the while loop. – Agaz Wani Feb 25 '17 at 16:32
  • Repeat your benchmark several times. Execution time is not reproducible (because of many caches) – Basile Starynkevitch Feb 25 '17 at 16:33
  • @BasileStarynkevitch I understand, but the difference is so huge (12 sec) – Agaz Wani Feb 25 '17 at 16:37
  • So run your entire program several times. Execution time can vary a lot (e.g. because of I/O & page cache) – Basile Starynkevitch Feb 25 '17 at 16:38
  • @BasileStarynkevitch Ok, do you feel any difference of having a time profile within the loop and out of the loop. – Agaz Wani Feb 25 '17 at 17:02
  • BTW, what operating system (& version), what compiler (& version), what optimization flag and what actual code are you benchmarking? How many times? Is your program multi-threaded? – Basile Starynkevitch Feb 25 '17 at 17:10
  • When it says 13 seconds, does that correspond with your wristwatch or wall clock? – Mike Dunlavey Feb 25 '17 at 17:17
  • @BasileStarynkevitch Linux `16.04`, `GCC c++17` , actually I am matching some 10 million strings of length 40 each. The function is running at least 180 million times and the actual time it takes to finish the job is `827` milliseconds which I check inside the while loop, and after finishing the while loop, the program seem to be holding for some time for some reason which I checked out of the while loop. – Agaz Wani Feb 25 '17 at 17:17
  • @MikeDunlavey Yes – Agaz Wani Feb 25 '17 at 17:18
  • 2
    Then I would consider that `high_resolution_clock` an academic curiosity. – Mike Dunlavey Feb 25 '17 at 17:19

1 Answers1

0

Couldn't it just be that your match_time is not initialized?

Adrien
  • 324
  • 1
  • 3
  • 10