在Windows上运行7.3-3.4版本的php-cli,在Linux上运行7.3.6,等式time()-$_SERVER["REQUEST_TIME_FLOAT"]
似乎相差1秒。.为什么?运行php -r 'sleep(4);var_dump(time()-$_SERVER["REQUEST_TIME_FLOAT"]);'
,我得到数字3.X,我希望这总是 返回一个数字> = 4,为什么我得到3.x数字?
hans@hans-lp17 ~
$ php -r 'sleep(4);var_dump(getrusage()["ru_utime.tv_sec"]);var_dump(time()-$_SERVER["REQUEST_TIME_FLOAT"]);'
int(0)
float(3.1129999160767)
hans@hans-lp17 ~
$ php -r 'sleep(4);var_dump(getrusage()["ru_utime.tv_sec"]);var_dump(time()-$_SERVER["REQUEST_TIME_FLOAT"]);'
int(0)
float(3.3659999370575)
hans@hans-lp17 ~
$ php -r 'sleep(4);var_dump(time()-$_SERVER["REQUEST_TIME_FLOAT"]);'
float(3.3789999485016)
hans@hans-lp17 ~
$ php -r 'sleep(4);var_dump(time()-$_SERVER["REQUEST_TIME_FLOAT"]);'
float(3.664999961853)
hans@hans-lp17 ~
$ php -r 'sleep(4);var_dump(time()-$_SERVER["REQUEST_TIME_FLOAT"]);'
float(3.3450000286102)
hans@hans-lp17 ~
$ php -r 'function f(){sleep(4);var_dump(time()-$_SERVER["REQUEST_TIME_FLOAT"]);}f();'
float(3.2279999256134)
hans@hans-lp17 ~
$ php -r 'function f(){sleep(4);var_dump(time()-$_SERVER["REQUEST_TIME_FLOAT"]);}f();'
float(3.0420000553131)
hans@hans-lp17 ~
$ php -r 'function f(){sleep(4);var_dump(time()-$_SERVER["REQUEST_TIME_FLOAT"]);}f();'
float(3.539999961853)
hans@hans-lp17 ~
$ php -r 'function f(){sleep(4);var_dump(time()-$_SERVER["REQUEST_TIME_FLOAT"]);}f();f();'
float(3.0999999046326)
float(7.0999999046326)
答案 0 :(得分:0)
..显然是一个精度和舍入问题,$_SERVER["REQUEST_TIME_FLOAT"]
的精度为微秒,而使用microtime(true)代替,time()的精度比该精度低1000000倍,我们得到:
hans@hans-lp17 ~
$ php -r 'sleep(4);var_dump(microtime(true)-$_SERVER["REQUEST_TIME_FLOAT"]);'
float(4)
hans@hans-lp17 ~
$ php -r 'sleep(4);var_dump(microtime(true)-$_SERVER["REQUEST_TIME_FLOAT"]);'
float(4)
hans@hans-lp17 ~
$ php -r 'sleep(4);var_dump(microtime(true)-$_SERVER["REQUEST_TIME_FLOAT"]);'
float(4.0009999275208)
hans@hans-lp17 ~
$ php -r 'sleep(4);var_dump(microtime(true)-$_SERVER["REQUEST_TIME_FLOAT"]);'
float(4.0009999275208)