Globally record the running time of the program fragment to find the breakpoint with much time consumed by the Program Logic

Source: Internet
Author: User

Copy codeThe 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

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.