6

我有一个由 JS 通过 JQuery 加载的 PHP 脚本$.ajax。我使用以下方法测量了 PHP 脚本的执行时间:

$start = microtime(); // top most part of code
// all other processes that includes AES decryption
$end = microtime(); // bottom part of code
file_put_contents('LOG.TXT','TIME IT TOOK: '.($end-$start)."\n",FILE_APPEND);

它测量的时间不到 1 秒。没有前置/附加 PHP 脚本。

在 JS$.ajax代码中,我通过以下方式测量了执行时间:

success: function(response) {
    console.log(date('g:i:s a') + ' time received\n');
    // all other processes including AES decryption
    console.log(date('g:i:s a') + ' time processed\n');
}

接收时间和处理时间相同。

但是,当我检查 Chrome 开发人员工具时,它声称 PHP 脚本加载了大约 8 秒

我测量这些东西的方式可能有什么问题?我确定 PHP 加载速度很快,但 Chrome 怎么会报告它花了超过 8 秒?

我正在使用本地主机,我的网络服务器很快,这是我唯一一次遇到这个问题。所有其他 AJAX 调用都很快。

4

1 回答 1

2

在 PHP 部分,确保您使用microtime(true)的是浮点数而不是字符串。对字符串使用减法可能会产生不正确的结果。


示例:http: //ideone.com/FWkjF2

<?php

// Wrong
$start = microtime();
sleep(3);
$stop  = microtime();
echo ($stop - $start) . PHP_EOL;  // Prints 8.000000000008E-5

// Correct
$start = microtime(true);
sleep(3);
$stop  = microtime(true);
echo ($stop - $start) . PHP_EOL;  // Prints 3.0000791549683

?>
于 2013-03-04T19:16:38.440 回答