title: C++ use std::chrono to measure execution of member functions in a nice way
body: <p>I want to optimize my application, especially the execution speed of certain functions.</p>
  
  <p>Imagine there is a class with some member functions</p>
  
  <pre><code>class Test
  {
  public:
      Test();
      virtual ~Test();
      int init(int arg1, double arg2);
  
  private:
      [...]
  </code></pre>
  
  <p>and in my constructor I call one of these methods</p>
  
  <pre><code>Test::Test()
  {
      [...]
      int value = init(1, 1.2);
  }
  </code></pre>
  
  <p>How can I measure the execution time of my method <code>init(...)</code> in a nice and clean way without breaking my program?</p>
  
  <p>At the moment I use following code</p>
  
  <pre><code>Test::Test()
  {
      [...]
      auto start = std::chrono::high_resolution_clock::now();
  
      int value = init(1, 1.2);
  
      auto stop = std::chrono::high_resolution_clock::now();
      std::chrono::duration&lt;double&gt; duration = stop - start;
      std::cout &lt;&lt; duration.count() * 1000 &lt;&lt; "ms\n";
  }
  </code></pre>
  
  <p>It works as expected but I think it is pretty messy and I want to have a "cleaner" solution.</p>
  
  <p>Is there a way to have some kind of function which takes a member function and other parameters like so</p>
  
  <pre><code>int value = countTime(function, arg1, arg2);
  </code></pre>
  
  <p>I don't know whether it is possible to pass the return value from <code>function()</code> to <code>countTime()</code> in order to don't interrupt the workflow of my code.</p>
  
  <p><strong>EDIT:</strong>
  This is my TimeMeasure class</p>
  
  <pre><code>namespace tools 
  {
      class TimeMeasure 
      {
      public:
          TimeMeasure() 
          {
              m_start = std::chrono::high_resolution_clock::now();
          }
  
          virtual ~TimeMeasure()
          {
              m_stop = std::chrono::high_resolution_clock::now();
              std::chrono::duration&lt;double, std::milli&gt; duration = m_stop - m_start;
              std::cout &lt;&lt; duration.count() &lt;&lt; "ms\n";
          }
  
      public:
          typedef std::chrono::time_point&lt;std::chrono::high_resolution_clock&gt; HighResClock;
  
      private:
          HighResClock m_start;
          HighResClock m_stop;
      };
  
      template &lt;typename T, typename F, typename... Args&gt;
      auto measure(T *t, F &amp;&amp;fn, Args... args)
      {
          tools::TimeMeasure timeMeasure;
          return (t-&gt;*fn)(std::forward&lt;Args&gt;(args)...);
      }
  }
  </code></pre>
  
  <p>and in my constructor <code>Test()</code> I use the function <code>measure</code> this way</p>
  
  <pre><code>Test()
  {
      [...]
      tools::measure(this, Test::init, filepath);
  }
  </code></pre>
  
  <p><code>int init(const std::string&amp; filepath) const</code> takes here a string to a file. So in my case it's just one argument</p>
  
  <p>Unfortunately I get a <code>invalid use of non-static member function 'int init(const string&amp;) const'</code> error</p>
  
  <p>I would wonder if a constructor is not a member function. So why do I get this error?</p>
  
  <p><strong>EDIT 2:</strong></p>
  
  <p>According to OznOg's answer I just forgot to hand in a pointer to my function.</p>
  
  <p>So this would be the correct function call</p>
  
  <pre><code>tools::measure(this, &amp;Test::init, filepath);
  </code></pre>