Segment to calculate execution time for PHP scripts

Source: Internet
Author: User
Tags php script

Many times we need to calculate the execution time of the PHP script to learn about the efficiency of the script, and so on. For example, there is a large section of PHP script, we need a fragment to get the script execution time method. First introduce the function to be used:

Timing functions
function runtime ($mode = 0) {
Static $t;
if (! $mode) {
$t = Microtime ();
Return
}
$t 1 = microtime ();
List ($m 0, $s 0) = Split ("", $t);
List ($m 1, $s 1) = Split ("", $t 1);
Return sprintf ("%.3f MS", ($s 1+ $m 1-$s 0-$m 0) *1000);
}

Runtime (); Timing starts
/*
PHP script to calculate
$result = 0;
for ($i = 0; $i < $i + +) {$result + = $i;} echo $result; */Echo runtime (1); Timing end and output timing result runtime (); Timing Start/*//php script to be calculated $result = 0; for ($i = 0; $i < $i + +) {$result + = $i;} echo $result; */Echo runtime (2); Timing end and output timing results

Microtime () function

The Microtime () function returns the current Unix timestamp and the number of microseconds.

Microtime (get_as_float), parameter get_as_float, if the get_as_float parameter is given and its value is equivalent to TRUE, the function returns a floating-point number.

<?php
Echo (Microtime ());
?>

If called without optional arguments, this function returns a string in the format "msec sec", where the SEC is the number of seconds since the Unix era (0:00:00 January 1, 1970 GMT) and msec is the microsecond portion. The two parts of a string are returned in seconds.

Program output:

0.25139300 1138197510

Now it's time to compute the timephased execution of the PHP script.

Segment to calculate execution time for PHP scripts

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.