title: In Python, how to time execution time of a function with an outer function that takes an inner function as an argument?
body: <p><em>Python 3.7 (WinOS, VS Code)</em></p>
  <h3>I WANT</h3>
  <p>I want to have a timer function in my library to use in a certain way. If there already is such a function available through <code>from something import what_i_need</code>, please let me know.</p>
  <p>The best way to use a function for me would be such that I can simply wrap whatever function which returns something in this timer function <code>time(...)</code> instead of in <code>print(...)</code>, so I don't have to change anything but that when I want to test execution time.</p>
  <pre class="lang-py prettyprint-override"><code>print(add(2, 3))
  &quot;&quot;&quot;output:
  5
  &quot;&quot;&quot;
  
  time(add(2, 3))
  &quot;&quot;&quot;output:
  ---
  Result: 5
  Execution time: 7.000000010748408e-07
  &quot;&quot;&quot;
  </code></pre>
  <p>...but it would also have to be acceptable (if it cannot be done the other way) to have to use like this:</p>
  <pre class="lang-py prettyprint-override"><code>time(add, 2, 3)
  </code></pre>
  <h3>I HAVE part 1: The correct result without a function</h3>
  <p>It's easy to time execution in a variety of ways (are any others better than <code>perf_counter</code>?). One such is the following:</p>
  <pre class="lang-py prettyprint-override"><code>from time import perf_counter
  
  t_start = perf_counter()
  print(&quot;---\nResult: &quot;, add(2, 3), &quot;\nExecution time: &quot;, perf_counter() - t_start, sep=&quot;&quot;)
  </code></pre>
  <h3>I HAVE part 2: A function without the correct result</h3>
  <p>I made a working function, that easily accepts any function in a way I like the most, but I came to realize it's useless, since the inner function is executed before the time is measured:</p>
  <pre class="lang-py prettyprint-override"><code>from time import perf_counter
  
  def time(function):
    t_start = perf_counter()
    print(&quot;---\nResult: &quot;, function, &quot;\nExecution time: &quot;, perf_counter() - t_start, sep=&quot;&quot;)
  
  time(add(2, 3))
  </code></pre>
  <h3>BONUS</h3>
  <p>If I could use it like <code>time(add(b=3, a=2))</code> or <code>time(add, b=3, a=2)</code> where <code>def add(a=0, b=0): return a + b</code> it would be even better.</p>