Nonsense not to say, directly to the Timeit function I wrote to share with you, the details are as follows:
/** * Compute The delay to execute a function a number of time * @param $count number of ' time ' tests would execu Te the given function * @param $function the function to test. Can is a string with parameters (ex: ' MyFunc (123, 0, 342) ') or a callback * @return float Duration in seconds (as a Flo
AT) */function Timeit ($count, $function) {if ($count <= 0) {echo "Error:count have to is more than zero";
return-1;
} $nbargs = Func_num_args ();
if ($nbargs < 2) {echo ' Error:no funciton! ';
Echo ' Usage: ';
echo "\ttimeit (count, ' function (param) ')";
echo "\te.g:timeit (0,2)"; return-1;
No function to TIME}//Generate callback $func = Func_get_arg (1);
$func _name = current (Explode ('), $func);
if (!function_exists ($func _name)) {echo ' Error:unknown function '; return-1;
Can ' t test unknown function} $str _cmd = ';
$str _cmd. = ' $start = Microtime (true); $str _cmd. = ' for ($i =0; $i < '. $count; $i + +) '. $func.';';
$str _cmd. = ' $end = Microtime (true);
$str _cmd. = ' Return ($end-$start); ';
Return eval ($str _cmd); }
Test your own write a root algorithm and the system's built-in root function execution time, as follows:
Take square root
function sqrt_nd ($num) {
$value = $num;
while (ABS ($value * $value-$num) > 0.001) {
$value = ($value + $num/$value)/2;
}
return $value;
}
Print Timeit (1000, ' sqrt_nd (5) ');
print "\ n";
Print Timeit (1000, ' sqrt (5) ');
The test results are as follows:
0.028280019760132
0.0041000843048096
Visible, the built-in root function is more than 6 times times faster than the custom root function ~ ~
The method used to detect function execution time in PHP
The Microtime () function in PHP can be implemented
The Microtime () function returns the current Unix timestamp and the number of microseconds.
Microtime (Get_as_float)
Parameter description
Get_as_float If the get_as_float argument is given and its value is equivalent to TRUE, the function returns a floating-point number.
This function is only available under an operating system that supports Gettimeofday () system calls.
For example:
<?php
$start _time = Microtime (true);
For ($i =1 $i <=1000; $i + +) {
echo $i. ' <br> ';
}
$end _time = Microtime (true);
echo ' Loop execution time is: '. ($end _time-$start _time). ' S ';
?>