-1

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.

bob.sacamento
  • 6,283
  • 10
  • 56
  • 115
  • 2
    duration does have a += operator. what are the errors? – Neil Butterworth Jan 18 '23 at 23:54
  • 2
    Semi-related note: Avoid using `high_resolution_clock` for timing stuff as is prizes resolution over pretty much everything else, including stability of the clock. Good reading on the topic: [What are the uses of std::chrono::high_resolution_clock?](https://stackoverflow.com/q/37426832/4581301). – user4581301 Jan 19 '23 at 00:01
  • 2
    First line in foo use `std::chrono::microseconds` instead of `duration`. `std::chrono::duration` class template takes two types **. `std::chrono::microseconds` is alias for `duration*signed integer type of at least 55 bits*/, std::micro>`. – M. Galib Uludag Jan 19 '23 at 00:04
  • @M.GalibUludag this is the answer, so you should post it as such (along with some bad semicolon use). Here is a live example of it fixed that you can use: http://coliru.stacked-crooked.com/a/2fc8310f8e3fdb98 – Fantastic Mr Fox Jan 19 '23 at 00:22
  • 2
    *"Trying to decipher the multitudinous error messages I get, it seems that [...]"* -- you realize that you are asking us to trust your deciphering of the error messages, right? So anything we learn from that is skewed by the quality of your deciphering. How confident are you that your deciphering is perfect? (Consider whether or not it led you to a solution...) It's a good idea to copy the exact error message into your question. Only the first error message (including its notes) is needed, and a code block is a good home for long error messages. – JaMiT Jan 19 '23 at 00:53
  • Also, if you declare `full_exec_time` as `std::chrono::high_resolution_clock::duration`, then you can remove `duration_cast` and do it only once when you need to "output" this value. – sklott Jan 19 '23 at 00:58
  • 2
    Another benefit of having the error message in your question: it makes the question easier to find by the next person with the same issue. For example, after I compiled your code to get the error message, I was able to find [Attempt to cast std::chrono::duration gives "rep cannot be a duration" compilation error](https://stackoverflow.com/questions/45684711/attempt-to-cast-stdchronoduration-gives-rep-cannot-be-a-duration-compilati) – JaMiT Jan 19 '23 at 01:02
  • 2
    Here's a chrono intro video tutorial: https://www.youtube.com/watch?v=P32hvk8b13M : cost 1h. Hope it helps. – Howard Hinnant Jan 19 '23 at 01:13
  • @NeilButterworth Yes, it does have a "+=" operator, which I found in a reference. The error is something to the effect that the "+=" operator doesn't exist for the two different duration types represented in the statement. Which completely confuses me, because I figure they must be the same type. – bob.sacamento Jan 19 '23 at 01:48

1 Answers1

3

First line in foo use std::chrono::microseconds instead of duration<microseconds>. std::chrono::duration class template takes two types <class Rep, class Period>.

std::chrono::microseconds is alias for duration</*signed integer type of at least 55 bits*/, std::micro>.

Fixed bad semicolon use in code: https://godbolt.org/z/7fYhq8KGW

M. Galib Uludag
  • 369
  • 2
  • 8