The Theme and Plugin Profiler uses a little known PHP feature known as ticks. Basically, this is an internal event that occurs every time a code block is executed, marked (roughly) by a function call or curly {} brackets.
The nice thing is, you can hook a function into a tick event. This enables you to determine at what point in the code you are and how much time has lapsed since last tick. This information can be used to determine how much time is spent in each function. By maintaining a stack of function calls it is also possible to determine how often a certain function is called.
A little more detail on the code
The code starts by declaring that the plugin want to use ticks.Note: if you research PHP ticks you will find sites stating that ticks have been deprecated as of PHP 5.3. This is not true. It was deprecated, but was then undeprecated. Ticks are available in PHP 7 as well. However, the Theme & Plugin Profiler has been tested with 5.3 only.
Next the earliest possible WordPress hook, muplugins_loaded, is used to register the function that is executed at every tick. This means that you must test your plugin as a regular plugin, because muplugins have already been loaded by the time the Theme & Plugin Profiler starts.
At every tick the native PHP function debug_backtrace is called. This returns information on the code currently being executed. For the moment, the plugin only uses the name of the current function. Also at every tick the current server time is retrieved.
If the current function name is the same as the previous function name, it is assumed that we’re still in the same function. The time elapsed since last tick is then added to the total time consumed by this function.
If the current function name is different from the previous function name, there are two possibilities. Either execution has returned from a subroutine or a subroutine has been called. A stack of function calls is maintained to establish which of these is true. In this way it is possible to keep track of how many times a function is called.
The code is not watertight
While this approach will accurately count how much time is spent in each function, the approach with the stack is not watertight.
First, if you call a function recursively, this wil definitely mess up the stack.
Second, if execution returns from a subroutine and immediately another subroutine is called, the ticks may miss the return and stack will think the second subroutine has been called from the first. When later on the return to the main function is registered, it is counted as a new call. So, the call count stated by the plugin may be inflated. For testing purposes you might insert a line like if (true==true) {echo '';} between subroutine calls to make sure a tick is registered within the main function.
The code generates errors
Unfortunately the debug_backtrace command generates a usort_warning. This is a known PHP bug. It’s just a warning, everything is still working fine (or, at least, should).
As a follow up of this warning PHP or WordPress will likely also throw a Cannot Modify Header Information error once you try to save the plugin’s options. Simply hitting backspace will fix this.
The output
As described the plugin collects a list of the functions that were executed, how often they were called and how much time they took in total. The plugin’s option page gives some filters to select and sort the results.
The plugin remains active until the very last moment to analyse as much code as possible. This means it starts printing output after the </body> tag has been generated. So, the page with the output table will not be valid html, but still should render fine.