Is there any function like clock() in c in php?
I want to measure the execution time for ph开发者_如何学Pythonp script~
Use microtime
. Make sure to look at example #2.
With microtime: example
mixed microtime ([ bool $get_as_float ] )
microtime()
returns the current Unix timestamp with microseconds. This function is only available on operating systems that support thegettimeofday()
system call.
As long as PHP is running on a Unix system, you can get a fairly accurate measurement of the time taken using the time(1)
command. See the manpage for usage and output information.
If you want to time a specific section of the script, for whatever reason, I would personally just copy/paste it into its own file and run time(1)
on that.
If you need to use the return value within a php script, you can use output generated by time(1)
from PHP by returning it into a string using either shell_exec()
or the backtick (`) operator. See the PHP manual for more information on that.
As for a native PHP implementation of the clock(3)
function, I don't believe such a thing exists and if it does I don't think it's part of the standard library.
The microtime
function used by other answers does something different to clock(3)
, as has been pointed out by Steve Jessop.
function get_microtime() {
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}
精彩评论