For a long time, ThinkPHP has needed to use the debug_start, debug_end methods and even the Debug class to complete those debugging functions. However, in the ThinkPHP 3.1 version, these complex functions have been replaced by a simple G method. This is not surprising. A gorgeous upgrade.
The G method includes two functions: marking position and interval statistics. Let’s take a look at the specific usage:
1. Mark the location
The first usage of G method is to mark the position, for example:
G('begin');
indicates that the current position is marked as the begin tag, and the execution time of the current position is recorded. If the environment supports it, the memory usage can also be recorded. G method markers can be called anywhere.
2. Running time statistics
After marking the position, we can call the G method again to perform interval statistics, for example:
G('begin'); // ...其他代码段 G('end'); // ...也许这里还有其他代码 // 进行统计区间 echo G('begin','end').'s';
G('begin','end') means counting the execution time from the begin position to the end position (unit is seconds). Begin must be a position that has been marked. If the end position has not been marked at this time, The current position will be automatically marked as the end tag, and the output result will be similar to:
0.0056s
The default statistical accuracy is 4 decimal places. If you feel that this statistical accuracy is not enough, you can also set it as:
G('begin','end',6).'s';
Possible output would become:
0.005587s
3. Memory overhead statistics
If your environment supports memory usage statistics, you can also use the G method to perform interval memory overhead statistics (unit is kb), for example:
echo G('begin','end','m').'kb';
The third parameter uses m to represent memory overhead statistics. The output result may be:
625kb
Similarly, If the end tag is not marked, the current position will be automatically marked as the end tag .
If the environment does not support memory statistics, this parameter is invalid and interval running time statistics will still be performed.
Forget about debug_start and debug_end, simplicity is the way to go, you know~