Globally record the running time of the program fragment to find the point where the program logic is time-consuming _ PHP Tutorial

Source: Internet
Author: User
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...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.