2013-07-17 41 views
0

我有下面的PHP代碼:速度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>'; 

} 

當空<td></td>位於我想放置以下:

$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>'; 

    } 

此作品在一定程度上,但頁面的加載時間顯着增加。我認爲說實話我的代碼需要整理。有關如何加快速度的建議?

+0

的瓶頸是'CS_REST_Campaigns :: get_lists_and_segments內()'和/或'CS_REST_Subscribers :: get_history()' - 不知道他們做什麼,他們是如何做到這一點有沒有辦法幫你加快速度。您應該查看分析器,例如http://www.xdebug.org/docs/profiler – DaveRandom

+0

感謝您的鏈接。這些文件直接來自廣告系列監視器提供的php包裝。無論如何,我還沒有改變過,所以他們不應該造成任何問題。 – danyo

+0

它看起來像是向外部API發出請求,如果這不是你的放緩原因,我會非常驚訝。您需要等待至少2個其他HTTP請求才能完成處理,然後才能繼續處理當前處理的請求。這些循環應該消耗不超過幾毫秒,如果他們處理的是大量的數據,那麼最可能的情況就是少得多。 – DaveRandom

回答

1

在我的代碼中沒有太多可以看到的,那就是我的CS_REST類中不存在函數調用,但我們不知道這些函數做了什麼,或者它們是否可以緩慢或優化。

有了這些信息,我唯一能看到的就是使用SplFixedArray類。如果您的陣列中有很多條目並對它們進行了大量操作,這將非常有用。基本上,它們類似於真正的數組,它們的索引總是一個整數,並且它們有一個固定的大小,這反過來又使它們更快使用。