Constructor start, destructor end timer in C++ The following is a short timing utility I've written for a larger project that I'm working on. The larger project is designed to be called (via ffi) from a Scala/Java codebase, which makes using standard profiling tools somewhat difficult - especially at the granularity that we want. 

Essentially, we want to be able to track roughly how long a few key functions are taking, and we want a low overhead way of doing so. The following code is my implementation of a timer class that starts timing when constructed, and then finishes (and reports data) when it destructs. 

I chose this way of doing things as it gives quite a succinct way of benchmarking a function, take for example, a function foo that we wanted to benchmark, all we would have to do is the following: 

void foo(/*some arguments*/) {
  auto timer = CSDSTimer("foo", "example");
  /*do some more stuff*/
}


I think this is quite a nice solution, but I'd be interested in feedback on my implementation, especially of my handling of streams, and whether there's any way to set the stream statically (see the commented out code). 

Thanks!

#pragma once

#include &lt;chrono&gt;
#include &lt;ctime&gt;
#include &lt;iostream&gt;

class CSDSTimer {

public:
  // Construct and start the timer
  CSDSTimer(std::string name, std::string context = "global",
            std::ostream &amp;stream = std::cout)
      : _name(name), _context(context), _default_str(&amp;stream) {
    _start = std::chrono::system_clock::now();
  }

  // Destruct and stop the timer, reporting the elapseed time
  ~CSDSTimer() {
    _end = std::chrono::system_clock::now();
    std::chrono::duration&lt;double&gt; elapsed_seconds = _end - _start;
    *_default_str &lt;&lt; "PROFILING_DATUM(\"" &lt;&lt; _name &lt;&lt; "\", \"" &lt;&lt; _context
                  &lt;&lt; "\", " &lt;&lt; elapsed_seconds.count() * 1000 &lt;&lt; ", \"C++\")"
                  &lt;&lt; std::endl;
  }

  // static void SetStream(std::ostream &amp;str) { _default_str = &amp;str; }

private:
  std::string _name;
  std::string _context;
  std::chrono::time_point&lt;std::chrono::system_clock&gt; _start, _end;
  std::ostream *_default_str;

  // static std::ostream *_default_str;
};
Design:

The problem I have is that you need to modify the thing you want to time (which means changing the timing).

  void functionIWantToTime()
  {
       CSDSTimer time("functionIWantToTime");

       // Stuff that takes time
  }


What would be better is a way to time a function without modifying it.

 CSDSTimer("functionIWantToTime", &amp;functionIWantToTime);


This also allows you to time random set of stuff that is not a function by using a lambda to combine them.

 CSDSTimer("Stuff", [](){
     function1();
     function2();
 )};


Code Review

Pass big parameters by const reference

To avoid copying objects pass by const reference.

CSDSTimer(std::string name, std::string context = "global", std::ostream &amp;stream = std::cout)
                  ^^^^^^ Add const&amp;  ^^^^^


Avoid holding pointers:

std::ostream *_default_str;


You pass by reference. Why not store a reference?

Prefer '\n' over std::endl

The difference is a flush. Flushing is usually sub-optimal; the stream will flush at the optimal time. Manual flushing usually just retards performance.

Timer Accuracy

I did not spot it. But @hoffmale suggested using a more accurate timer. I agree that is probably a good idea (you have nothing to lose with more accuracy).