title: Total time in different parts of recursive function
body: <p>I am new to C++ and I need to measure the total time for different parts of a recursive function. A simple example to show where I get so far is:</p>
  <pre><code>#include &lt;iostream&gt;
  #include &lt;unistd.h&gt;
  #include &lt;chrono&gt;
  using namespace std;
  using namespace std::chrono;
  
  int recursive(int);
  void foo();
  void bar();
  
  int main() {
      int n = 5;  // this value is known only at runtime
      int result = recursive(n);
      return 0;
  }
  
  int recursive(int n) {
      auto start = high_resolution_clock::now();
      if (n &gt; 1) { recursive(n - 1); n = n - 1; }
      auto stop = high_resolution_clock::now();
      auto duration_recursive = duration_cast&lt;microseconds&gt;(stop - start); 
  
      cout &lt;&lt; &quot;time in recursive: &quot; &lt;&lt; duration_recursive.count() &lt;&lt; endl;
      //
      // .. calls to other functions and computations parts I don't want to time
      //
      start = high_resolution_clock::now();
      foo();
      stop = high_resolution_clock::now();
      auto duration_foo = duration_cast&lt;seconds&gt;(stop - start);
      cout &lt;&lt; &quot;time in foo: &quot; &lt;&lt; duration_foo.count() &lt;&lt; endl;
      //
      // .. calls to other functions and computations parts I don't want to time
      //
      start = high_resolution_clock::now();
      bar();
      stop = high_resolution_clock::now();
      auto duration_bar = duration_cast&lt;seconds&gt;(stop - start);
      cout &lt;&lt; &quot;time in bar: &quot; &lt;&lt; duration_bar.count() &lt;&lt; endl;
  
      return 0;
  }
  
  void foo() { // a complex function
  sleep(1);
  }
  
  void bar() { // another complex function
  sleep(2);
  }
  </code></pre>
  <p>I want the <strong>total</strong> time for each of the functions, for instance, for foo() it is 5 seconds, while now I always get 1 second. <strong>The number of iterations is known only at runtime</strong> (<code>n=5</code> here is fixed just for simplicity).</p>
  <p>To compute the <strong>total</strong> time for each of the functions I did try replacing the type above by using <code>static</code> and accumulate the results but didn't work.</p>