I need to get a running total of all the time spent in a particular C++ function that is called a few million times (or more). I am trying to use C++ chrono
, which I have no experience with. This code seemed to me like it would certainly work, but it doesn't compile:
#include <ctime>
#include <ratio>
#include <chrono>
int foo() {
static duration<microseconds> full_exec_time{};
auto start_time = high_resolution_clock::now():
// do a bunch of stuff
auto stop_time = high_resolution_clock::now();
auto exec_time = duration_cast<microseconds>(stop_time - start_time);
full_exec_time += exec_time; // problem right here
}
Apparently the problem is with incrementing full_exec_time
. Trying to decipher the multitudinous error messages I get, it seems that chrono
does not have a "+=" operator that works with the two types of durations
present in the expression. This is a surprise to me, since I would assume that they would be of the same type. Anyway, anyone know how to do what I am trying to do here? This seems to me so straightforward that it would be widely documented, but I can't find anything on it anywhere. There's lots out there (and on stack overflow) about adding durations
to time points
, but not much of anything about adding two durations
together.