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.