Q:

Function using time.time() to return execution time of another passed function always returns 0.0 (Python)

I've tried to write a function which will run another function passed to it, and return its execution time.
Could someone please explain to me why the following always prints 0.0 seconds?
To clarify, the 'func' in my definition IS calling func without having to add parentheses. I'm seeing printed analysis outputs from func when calling run_and_return_execution_time(func). I'm running this in a Jupyter notebook at the moment - not sure if that makes any difference.
import time

def run_and_return_execution_time(func):
    t_start = time.time()
    func
    return time.time() - t_start

t_execution = run_and_return_execution_time(func)
print('Execution time: ',t_execution)

The function which I am trying to time is definitely executing, and prints around 44 seconds if I time with the following approach instead:
t_start = time.time()
func()
t_end = time.time()
print('Execution time: ',t_end - t_start)

I'm aware there are other ways to do this like using cProfile, but I'd be interested to learn what's going on here.

A:

You should add parantheses to the function that you are passing. Otherwise the function will not be executed. Your code should look like this:
import time

def run_and_return_execution_time(func):
    t_start = time.time()
    # Added the parantheses to the function: 
    func()
    return time.time() - t_start

t_execution = run_and_return_execution_time(func)
print('Execution time: ',t_execution)

