|
|
 |
microtime (PHP 3, PHP 4 , PHP 5) microtime --
返回当前 UNIX 时间戳和微秒数 说明string microtime ( void )
返回格式为“msec sec”的字符串,其中 sec 是当前的 Unix
时间戳,msec 是微秒部分。本函数仅在支持 gettimeofday()
系统调用的操作系统下可用。
字符串的两部分都是以秒为单位返回的。
例子 1. microtime() 例子 |
<?php
function getmicrotime(){
list($usec, $sec) = explode(" ",microtime());
return ((float)$usec + (float)$sec);
}
$time_start = getmicrotime();
for ($i=0; $i < 1000; $i++){
//do nothing, 1000 times
}
$time_end = getmicrotime();
$time = $time_end - $time_start;
echo "Did nothing in $time seconds";
?>
|
|
参见 time()。
mcq at supergamez dot hu
12-Dec-2000 02:47
if you want to measure runtime of some code, and the result is really
relevant, then keep in mind the followings:
1. you should only measure
the time the code runs. This means if you use functions to make a double
from microtime, then get begin time, run the code, get end time, and only
_after_ this do conversion and computing. This is relevant for short
cycles.
2. if runtime is very small, you can put the code in a loop and
run it for 100 or 1000 times. The more you run it the more accurate the
value will be. Do not forget to divide the runtime by the times you ran
it.
3. if you are measuring a loop, then you do not actually measure
the runtime of one cycle. Some caching may occur which means you will not
get one cycle's runtime. For a short code, this can eventually result in
big differences. Use looping only if the code is long and
comlicated.
4. your runtime will be highly affected by the load of the
server, which may be effected by many things - so, always run your runtime
measuering multiple times, and, when comparing two methods, for e.g., then
measure their runtimes one after the other, so I mean do not use values
from yesterday, the circumstances may strongly affect your
measuring.
Trapeer
| |