php中的基准代码

php中的基准代码,php,Php,我有一些php代码 代码中的某些内容(很大一部分代码)导致页面加载时间超过30秒 有没有一种简单的方法可以找出代码的哪一部分在执行此操作?如果您使用的是Zend Studio,您可以使用他们的分析器。或使用xdebug进行配置。 否则,您可以在函数调用周围放置计时器以缩小搜索范围 差不多 $startTime = microtime(); ... code to execute ... $timeSpent = microtime() - $startTime; 您还可以使用PECL APD扩

我有一些php代码

代码中的某些内容(很大一部分代码)导致页面加载时间超过30秒


有没有一种简单的方法可以找出代码的哪一部分在执行此操作?

如果您使用的是Zend Studio,您可以使用他们的分析器。或使用xdebug进行配置。 否则,您可以在函数调用周围放置计时器以缩小搜索范围

差不多

$startTime = microtime();
... code to execute ...
$timeSpent = microtime() - $startTime;
您还可以使用PECL APD扩展

<?php
apd_set_pprof_trace();

... rest of your code ...

祝你好运

尝试对xdebug使用分析/跟踪:
并且

您只需在PHP中按块检查代码的执行时间,如下所示:

start_time = get_time_now
<execution block 1>
execution_time = get_time_now - start_time

start_time = get_time_now
<execution block 2>
execution_time = get_time_now - start_time
start\u time=get\u time\u now
执行时间=立即获取时间-开始时间
开始时间=立即获取时间
执行时间=立即获取时间-开始时间
因此,每次执行特定的代码块时,它都会在几秒钟内被检查并打印到屏幕上。您需要做的是将计算添加到代码的每一块中,以测试哪一块正在减慢页面速度。我编写了一个示例代码供您参考:

<?php
    $time_start = microtime(true);
    // Execution 1
    usleep(300); // say it sleeps for a while, like executing something
    $time = microtime(true) - $time_start;
    echo "Execution 1 is done in $time seconds \n";

    $time_start = microtime(true);
    // Execution 2
    usleep(300); // say it sleeps for a while, like executing something
    $time = microtime(true) - $time_start;
    echo "Execution 2 is done in $time seconds \n";

    $time_start = microtime(true);
    // Execution 3
    usleep(3000000); // this gonna slow down your page
    $time = microtime(true) - $time_start;
    echo "Execution 3 is done in $time seconds";
    echo "And it's slowing down your page \n";
?>

总是有打印语句!您的代码是否在数据库中查询?执行sql聚合函数,如count()或avg()?有几种商业和开源工具。建议您搜索“性能评测php”可能重复的
<?php
    $time_start = microtime(true);
    // Execution 1
    usleep(300); // say it sleeps for a while, like executing something
    $time = microtime(true) - $time_start;
    echo "Execution 1 is done in $time seconds \n";

    $time_start = microtime(true);
    // Execution 2
    usleep(300); // say it sleeps for a while, like executing something
    $time = microtime(true) - $time_start;
    echo "Execution 2 is done in $time seconds \n";

    $time_start = microtime(true);
    // Execution 3
    usleep(3000000); // this gonna slow down your page
    $time = microtime(true) - $time_start;
    echo "Execution 3 is done in $time seconds";
    echo "And it's slowing down your page \n";
?>