2012-11-02 210 views
-2

好吧,基本上我設置了一個查詢來連接到兩個表並獲取所有信息,然後使用幾個循環來顯示我的網頁的某個部分。在緩慢的例子是在這裏:http://sage-scape.com/hiscores/compare.ws?user1=kyle 現在我的代碼有點大,但這可能是問題,我將兩個函數結合在一起,因爲我認爲這會加快過程,它沒有幫助太好。代碼如下:如何優化此代碼?

function compare($user1) { 
    include "mysql.ws"; 
    $user1 = str_replace(" ", "_", $user1); 
    $query = mysql_query("SELECT a.*, b.* FROM skills a JOIN activities b ON(a.playerName LIKE b.playerName) WHERE a.playerName LIKE '".fixName($user1)."' LIMIT 1") or die(mysql_error()); 

    $row = mysql_fetch_array($query); 
    $overall['lvl'] = 0; 
    $overall['xp'] = 0; 
    for ($i = 1; $i <=25; $i++){ 
     $xptype = findType($i)."xp"; 
     $overall["lvl"] += getLevelForXP($row[$xptype]); 
     $overall["xp"] += $row[$xptype]; 
    } 
     //updateDonator($user1); 

    echo'<div class="OrnamentalBoxBg"><div class="OrnamentalBoxContent"> 
     <div class="header"> 
     <div class="left"> 
     <span class="columnLevel">Level</span> 
     <span class="columnTotal">Total XP</span> 
     <span class="columnRank">Rank</span> 
     </div></div> 
     <div class="row" data-skill="combat" data-member="false"> 
     <div class="left"> 
     <a href="http://sage-scape.com/hiscores/overall.ws?category_type=0&amp;table=0&amp;rank='.findRank($user1,"0").'#'.findRank($user1,"0").'"> 
     <span class="columnLevel"> 
     '.dots($overall["lvl"]).' 
     </span> 
     </a> 
     <a href="http://sage-scape.com/hiscores/overall.ws?category_type=0&amp;table=0&amp;rank='.findRank($user1,"0").'#'.findRank($user1,"0").'"> 
     <span class="columnTotal"> 
     '.dots($overall["xp"]).' 
     </span> 
     </a> 
     <a href="http://sage-scape.com/hiscores/overall.ws?category_type=0&amp;table=0&amp;rank='.findRank($user1,"0").'#'.findRank($user1,"0").'"> 
     <span class="columnRank"> 
     '.findRank($user1,"0").' 
     </span> 
     </a> 
     </div> 
     <div class="middle"> 
     <img src="http://sage-scape.com/hiscores/img/overall.png?2" alt="" title=""> 
     <span class="statName"><span><span><span>Overall</span></span></span></span> 
     </div> 
     </div> 
    '; 
    for ($i = 1; $i <=25; $i++){ 
     $xptype = findType($i)."xp"; 
     if($i == 25){ 
      echo' 
       <div class="row last-row" data-skill="'.findSkill($i).'" data-member="'.member($i).'"> '; 

     } else { 
      echo' 
       <div class="row" data-skill="'.findSkill($i).'" data-member="'.member($i).'"> '; 
     } 
     echo' 
      <div class="left"> 
      <a href="http://sage-scape.com/hiscores/overall.ws?category_type=0&amp;table='.$i.'&amp;rank='.findRank($user1,"$i").'#'.findRank($user1,"$i").'"> 
      <span class="columnLevel">'; 
      if($i == 25){ 
       echo' 
        '.$row["Dungeoneeringlvl"].' 
        </span>'; 
      }else{ 
       echo' 
        '.getLevelForXP($row[$xptype],"$i").' 
        </span>'; 
      } 
      echo' 
      </a> 
      <a href="http://sage-scape.com/hiscores/overall.ws?category_type=0&amp;table='.$i.'&amp;rank='.findRank($user1,"$i").'#'.findRank($user1,"$i").'"> 
      <span class="columnTotal"> 
      '.dots($row["$xptype"],"$i").' 
      </span> 
      </a> 
      <a href="http://sage-scape.com/hiscores/overall.ws?category_type=0&amp;table='.$i.'&amp;rank='.findRank($user1,"$i").'#'.findRank($user1,"$i").'"> 
      <span class="columnRank"> 
      '.findRank($user1,"$i").' 
      </span> 
      </a> 
      </div> 
      <div class="middle"> 
      <img src="http://sage-scape.com/hiscores/img/xp_'.fixName(findType($i)).'_total.png?2" alt="" title="" /> 
      <span class="statName"><span><span><span>'.findType($i).'</span></span></span></span> 
      </div> 
      </div> 
     '; 
    } 
    echo' 
    </div></div> 
<div class="OrnamentalBoxBottom"></div> 
</div> 
<div class="OrnamentalBox Width923 SegmentTwo" id="activityStats"> 
<div class="OrnamentalBoxTop"></div> 
<div class="OrnamentalBoxTitle"><div class="Centre1"><div class="Centre2"> 
<h3 class="Gradient NoFlourish Centre"><span class="spacing" aria-hidden="true">Activity Stats</span> 
<span class="G0">Activity Stats</span> 
<span class="G1" aria-hidden="true">Activity Stats</span> 
<span class="G2" aria-hidden="true">Activity Stats</span> 
<span class="G3" aria-hidden="true">Activity Stats</span> 
<span class="G4" aria-hidden="true">Activity Stats</span> 
<span class="G5" aria-hidden="true">Activity Stats</span> 
<span class="G6" aria-hidden="true">Activity Stats</span> 
</h3></div></div></div> 
<div class="FlatHeader playerNames"> 
<div class="left"> 
'.BBCode($user1).'</div>'; 
    if(!empty($_GET["user2"])){ 
     echo' 
      <div class="right"> 
      '.BBCode($_GET["user2"]).' 
      </div> 
      '; 
    } 
echo' 
</div> 
<div class="playerAvatars"> 
<div class="left"> 
<img src="http://services.runescape.com/m=avatar-rs/'.$user11.'/chat.png?cachebust=1342804665991" alt="" title=""> 
</div>'; 

if(!empty($_GET["user2"])){ 
    echo' 
     <div class="right"> 
     <img src="http://services.runescape.com/m=avatar-rs/'.$_GET["user2"].'/chat.png?cachebust=1342804665991" alt="" title=""> 
     </div> 
      '; 
} 
echo' 
</div>'; 
    echo' 
     <div class="OrnamentalBoxBg"><div class="OrnamentalBoxContent"> 
     <div class="header"> 
     <div class="left"> 
     <span class="columnScore">Score</span> 
     <span class="columnRank">Rank</span> 
     </div> 
     </div>'; 
    for ($i = 0; $i < 7; $i++){ 
     $xptype = findActivityP($i); 
     if($i == 6){ 
      echo' 
       <div class="row last-row">'; 
     } else { 
      echo' 
       <div class="row">'; 
     } 
     echo' 
     <div class="left"> 
     <a href="http://sage-scape.com/hiscores/overall.ws?category_type=1&amp;table='.$i.'&amp;rank='.findARank($user1,"$i").'#'.findARank($user1,"$i").'"> 
     <span class="columnScore"> 
     '.$row["$xptype"].' 
     </span> 
     </a> 
     <a href="http://sage-scape.com/hiscores/overall.ws?category_type=1&amp;table='.$i.'&amp;rank='.findARank($user1,"$i").'#'.findARank($user1,"$i").'"> 
     <span class="columnRank"> 
     '.findARank($user1,"$i").' 
     </span> 
     </a> 
     </div> 
     <div class="middle"> 
     <img src="http://sage-scape.com/hiscores/img/'.findActivityP($i).'.png" alt="" title=""> 
     <span class="statName"><span><span><span>'.findActivity($i).'</span></span></span></span> 
     </div> 
     </div> 
     '; 
    } 

} 

技能表包含51列playerName和25種不同的xp和lvl技能。活動包含8列用戶和一些活動。我有幾個循環,所以代碼不是太大,但有人可以幫我優化這個,所以它會加載更快?

+0

查找重構。通常你想分解成更小的代碼段不大。此外,我會建議在代碼中的不同位置打印出時間以確定時間的使用位置。這將幫助你調試。我不認爲你會發現有太多的人會爲你調試它。所以除非這是一個明顯的錯誤,否則你可能不會得到太多的答案。 – Codeguy007

+1

可能重複的[PHP文件無法輸入部分代碼](http://stackoverflow.com/questions/11575531/php-file-cannot-enter-some-part-of-code) - 您的問題可能是非法的,主題,重複的建議只是一個類似的問題的鏈接,可能或不會幫助你解決你的問題。不過,這些問題最好在http://codereview.stackexchange.com/上提問。 – hakre

+0

如何計算代碼? –

回答

1

我的第一個想法是:措施措施措施!

沒有得到各部分的測量結果,你不知道哪個部分很慢。先嚐試一下。

一般而言,您會獲取一些數據,然後搜索以顯示相應的值。當你知道什麼是緩慢的,如果它的數據量

  1. 想想存儲這些擡頭值(高速緩存)
  2. 你可以使用其他資源,如DB做一下跌宕返回平板原數據你只是渲染?
  3. 您是否只獲取需要的數據,或者只是將數據庫加載到內存中才將其丟棄?

認真 - 做一些分析。