Executes the given function and measures how long the execution takes.