Timing a code block

I am trying to run a custom application with px4 autopilot on Pixhawk 4. I need to time need to execute certain portions of the code. There are two ways how we can do that:

  1. Output a message to log with a timestamp
  2. Read system time or clock at the beginning and at the end of the code block.

Which would be the better way to do? Or is there any other way to do the same?

Thanks

You can use hrt_absolute_time() to get a uint64_t with the microseconds since boot.
Alternative, for profiling you can use the perf counters.