Globally record the running time of the program fragment to find the breakpoint that consumes a lot of time in the program logic. The copy code is as follows: defines the global variable record time $ _ timer_id0; the function sets the global variable to record the running time of each breakpoint functionmakeTimer ($ notes, $ onOff
The code is as follows:
// Define the record time of global variables
$ _ Timer_id = 0;
// Set global variables to record the running time of each breakpoint
Function makeTimer ($ notes, $ onOff = FALSE)
{
If ($ onOff)
{
Global $ _ timer_id;
$ GLOBALS ['timer'] [$ _ timer_id] [0] = microtime (TRUE );
$ GLOBALS ['timer'] [$ _ timer_id] [1] = $ notes;
$ _ Timer_id ++;
}
}
// Output the global running time
Function traceTimer ()
{
$ Timer_str = '';
$ G_timer = count ($ GLOBALS ['timer'])-1;
If ($ G_timer> 0)
{
For ($ I = 0; $ I <$ G_timer; $ I ++)
{
$ Dif_time = number_format ($ GLOBALS ['timer'] [$ I + 1] [0]-$ GLOBALS ['timer'] [$ I] [0]), 3 );
$ Timer_str. = 'dif: '. $ dif_time.'. $ GLOBALS ['timer'] [$ I] [1]. "\ n ";
}
$ Dif_time = number_format (microtime (TRUE)-$ GLOBALS ['timer'] [$ G_timer] [0]), 3 );
$ Timer_str. = 'dif: '. $ dif_time.'. $ GLOBALS ['timer'] [$ G_timer] [1]. "\ n ";
}
Return $ timer_str;
}
Usage:
// Start time
MakeTimer ('line: '. _ LINE __);
$ Imgstrpos = strpos ($ str, 'maketimer ('line: '. _ LINE __);
$ Str_p = substr ($ str_noimg, 0, $ imgstrpos );
MakeTimer ('line: '. _ LINE __);
$ Str_n = substr ($ str_noimg, $ imgstrpos, strlen ($ str_noimg ));
MakeTimer ('line: '. _ LINE __);
$ Pst_exc_imgs = $ str_p. ''. $ str_n ."";
MakeTimer ('line: '. _ LINE __);
// Record to log
Error_log (traceTimer (), 3, '/tmp/'. basename (_ FILE _). '. log ');
// Or directly output
Echo traceTimer ();
At
The http://www.bkjia.com/PHPjc/322677.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/322677.htmlTechArticle code is as follows: // defines the global variable record time $ _ timer_id = 0; // The function sets global variables to record the time required for running each breakpoint function makeTimer ($ notes, $ onOff...