declare (ticks=1); register_tick_function( 'do_profile' ); register_shutdown_function( 'show_profile' ); $profile = array (); $last_time = microtime(true); a(); function do_profile() { global $profile , $last_time ; $bt = debug_backtrace(); if ( count ( $bt ) <= 1) { return ; } $frame = $bt [1]; unset( $bt ); $function = $frame [ 'function' ]; if (!isset( $profile [ $function ])) { $profile [ $function ] = array ( 'time' => 0, 'calls' => 0 ); } $profile [ $function ][ 'calls' ]++; $profile [ $function ][ 'time' ] += (microtime(true) - $last_time ); $last_time = microtime(true); } function show_profile() { global $profile ; print_r( $profile ); } function a() { usleep(50 * 1000); b(); } function b() { usleep(500 * 1000); c(); } function c() { usleep(5000 * 1000); } |
输出:
Array
(
[a] => Array
(
[time] => 0.0511748790741
[calls] => 2
)
[b] => Array
(
[time] => 0.500598907471
[calls] => 2
)
[c] => Array
(
[time] => 5.00052690506
[calls] => 1
)
)