加快php加载时间

加快php加载时间,php,foreach,Php,Foreach,我有以下php代码: // Add the unsubscribers to an array $unsubs = array(); foreach($unsubscribers->response->Results as $entry2) { $unsubs[] = $entry2->EmailAddress; } // Loop through the subscribers foreach($result->response->Results as

我有以下php代码:

// Add the unsubscribers to an array
$unsubs = array();
foreach($unsubscribers->response->Results as $entry2) {
    $unsubs[] = $entry2->EmailAddress;
}

// Loop through the subscribers
foreach($result->response->Results as $entry) { 

    echo '<tr class="'. $entry->EmailAddress.'">';      
    echo '<td>'. $entry->EmailAddress.'</td>';
    echo '<td></td><td>';

    // If the subscriber is in our unsubscriber array, output the email again
    if(in_array($entry->EmailAddress, $unsubs)) { 
        echo $entry->EmailAddress;
    }

    echo '</td><td></td>';
    echo '<td></td>';
    echo '<td></td>';
    echo '</tr>';

}
//将取消订阅者添加到数组中
$unsubs=array();
foreach($unsubscribers->response->Results as$entry2){
$unsubs[]=$entry2->EmailAddress;
}
//在订阅者中循环
foreach($result->response->Results as$entry){
回声';
回显“.$entry->EmailAddress.”;
回声';
//如果订户在我们的取消订户阵列中,请再次输出电子邮件
如果(在数组中($entry->EmailAddress,$unsubs)){
echo$entry->EmailAddress;
}
回声';
回声';
回声';
回声';
}
在空的
所在的位置,我想放置以下内容:

$getlists = new CS_REST_Campaigns($_POST['campaign_id'], $auth);
$getlistsresult = $wrap->get_lists_and_segments();
    foreach($getlistsresult->response->Lists as $list) {
        //echo $list->ListID;
    } 

$wrapcount = new CS_REST_Subscribers($list->ListID, $auth);
$resultcount = $wrapcount->get_history($entry->EmailAddress);   


    foreach($resultcount->response as $entrycount) { 

        $counts = array();

foreach($entrycount->Actions as $actions) {
    if(!isset($counts[$actions->Event])) {
        $counts[$actions->Event] = 0;
    }
    ++$counts[$actions->Event];
}


    echo '<td>';
    if ($counts['Click']){echo $counts['Click'];} 
    echo '</td>';

    echo '<td>';
    if ($counts['Bounce']){echo 'Yes';}
    echo '</td>';

    echo '<td>';
    if ($counts['Open']){echo $counts['Open'];} 
    echo '</td>';

    }
$getlists=new CS\u REST\u活动($\u POST['campaign\u id',$auth);
$getlistsresult=$wrap->get_List_和_segments();
foreach($getlistsresult->response->list as$list){
//echo$list->ListID;
} 
$wrapcount=新的CS\u REST\u订阅者($list->ListID,$auth);
$resultcount=$wrapcount->get_history($entry->EmailAddress);
foreach($resultcount->response as$entrycount){
$counts=array();
foreach($entrycount->Actions as$Actions){
如果(!isset($counts[$actions->Event])){
$counts[$actions->Event]=0;
}
++$counts[$actions->Event];
}
回声';
如果($counts['Click']){echo$counts['Click'];}
回声';
回声';
如果($counts['Bounce']){echo'Yes';}
回声';
回声';
如果($counts['Open']){echo$counts['Open'];}
回声';
}

这在一定程度上是可行的,但是页面的加载时间大大增加了。老实说,我想我的代码需要整理一下。有没有关于如何加速的建议?

我看不出你的代码中有多少明显未优化的地方,有一些我不知道的函数调用,来自你的CS_REST类,但我们不知道这些函数做什么,或者它们是否可以变慢或优化


有了这些信息,我能看到的唯一可能对您有所帮助的就是使用这个类。如果数组中有很多条目,并且对它们执行了很多操作,那么这将非常有用。基本上,它们与实际数组类似,其索引始终是整数,并且具有固定大小,这反过来使它们使用起来更快。

瓶颈在
CS\u REST\u活动::get\u列表和\u段()
和/或
CS\u REST\u订阅者::get\u history()
-如果不知道他们在做什么以及如何做,就无法帮助您加快速度。您应该查看一个探查器,例如感谢链接。这些文件直接来自CampaignMonitor提供的php包装。我没有改变这些,所以他们不应该造成任何问题。看起来他们正在向外部API发出请求,如果这不是你减速的原因,我会非常惊讶。您正在等待至少2个其他HTTP请求完全处理,然后才能继续处理当前正在处理的请求。循环消耗的时间不应超过几毫秒,如果它们处理大量数据,很可能会少很多。