Php 分组期间的脚本执行时间

Php 分组期间的脚本执行时间,php,Php,移动代码行,使其真正有意义: <?php $urls = array( 'us/1', 'us/2', 'gb/1', 'gb/2', 'de/1', 'de/2', ); $current_country = ''; foreach ($urls as $url) { $sleep_sec = rand(1, 3); printf('Proccessed url <b>%s</b> in %s s

移动代码行,使其真正有意义:

<?php

$urls = array(
    'us/1',
    'us/2',
    'gb/1',
    'gb/2',
    'de/1',
    'de/2',
);

$current_country = '';
foreach ($urls as $url) {
    $sleep_sec = rand(1, 3);
    printf('Proccessed url <b>%s</b> in %s sec%s', strtoupper($url), $sleep_sec, '<br/>');

    $country = substr($url, 0, 2);
    if ($current_country !== $country) {
        $start_at = microtime(true);
        $end_at = microtime(true) - $start_at;

        $logs[] = sprintf('Proccessed country <b>%s</b> in %s sec', strtoupper($country), $end_at);
    }
    $current_country = $country;

    sleep($sleep_sec);
}

echo str_repeat('<br>', 2);

foreach($logs as $log) {
    echo $log;
    echo '<br>';
}
$start\u at=microtime(真);
foreach($url作为$url){
$sleep_sec=rand(1,3);
睡眠($sleep_秒);
printf('s秒%s中处理的url%s',strtoupper($url),$sleep_sec,
); $country=substr($url,0,2); $current_country=$country; 如果($current_country!=$country){ $end_at=微时间(真)-$start_at; $logs[]=sprintf('s秒内处理的国家%s',strtoupper($country),$end_at); $start_at=microtime(真); } }
Aight,这就是重点,我开始键入应该放在哪里的内容,但最后,我发现实际上将所有行放在正确的位置会更容易;)
$start_at = microtime(true);
foreach ($urls as $url) {
    $sleep_sec = rand(1, 3);
    sleep($sleep_sec);
    printf('Proccessed url <b>%s</b> in %s sec%s', strtoupper($url), $sleep_sec, '<br/>');

    $country = substr($url, 0, 2);
    $current_country = $country;
    if ($current_country !== $country) {
        $end_at = microtime(true) - $start_at;
        $logs[] = sprintf('Proccessed country <b>%s</b> in %s sec', strtoupper($country), $end_at);
        $start_at = microtime(true);
    }
}