如何通过代码加速?

时间:2013-02-09 11:10:19

标签: php performance cycle

有没有办法加快我的代码?加载大约需要15秒...我真的没有办法减少我的代码......我只是考虑将值插入数据库,因此用户不必每次都加载新的信息..但是问题是我的cron每小时只允许1次加载...通过在每次加载时加载新信息它给我提供了新的信息..

$q1=mysql_query("SELECT * FROM isara");

while($r1=mysql_fetch_array($q1)){

        $named=$r1['name'];
        $idd=$r1['id'];
        $descd=$r1['desc'];
        $online=check_online($named);

        $char = new Character($r1['name'],$r1['id'],$r1['desc']);

        if($online == "online"){
                $char->rank = $i++;    
        }
        else{
                $char->rank = 0;
        }

        $arr[] = $char;

}
?>

<br />
<h2 style="color:green">Online enemies</h2>

<?php
foreach ($arr as $char) {
        if($char->rank>=1){
                echo "<a style=\"color:green\" href=\"http://www.tibia.com/community/?subtopic=characters&name=$char->name\">";
                echo $char->name." ";
                echo "</a>";
                echo level($char->name)."<b> ";
                echo vocation($char->name)."</b> (<i>";
                echo $char->desc." </i>)<br />";
        }
}
?>
<br /> 
<h2 style="color:red">Offline enemies</h2>
<?php
foreach ($arr as $char) {
        if($char->rank==0){
                echo "<a style=\"color:red\" href=\"http://www.tibia.com/community/?subtopic=characters&name=$char->name\">";
                echo $char->name." ";
                echo "</a>";
                echo level($char->name)."<b> ";
                echo vocation($char->name)."</b> (<i>";
                echo $char->desc." </i>)<br />";
        }
}

?>

3 个答案:

答案 0 :(得分:0)

正如我在评论中写的那样,为数据库中的每个名称取一次而不是一次。

我的评论的伪代码:

users = <get users from database>
webpage = <get webpage contents>

for (user in users)
    <check if user exists in webpage>

答案 1 :(得分:0)

正如评论中所提到的那样,你正在为数据库中的每个条目调用一个网页,假设每次调用大约2秒钟,它会让你失去很多。

为什么不一次调用页面并将其内容作为参数传递给check_online()函数,这样你的代码看起来就像这样会加速它的速度:

$content=file_get_contents("http://www.tibia.com/community/?subtopic=worlds&worl‌​d=Isara",0);
$q1=mysql_query("SELECT * FROM isara");

while($r1=mysql_fetch_array($q1)){

    $named=$r1['name'];
    $idd=$r1['id'];
    $descd=$r1['desc'];
    $online=check_online($named,$content);

    $char = new Character($r1['name'],$r1['id'],$r1['desc']);

    if($online == "online"){
            $char->rank = $i++;    
    }
    else{
            $char->rank = 0;
    }

    $arr[] = $char;

}
?>

<br />
<h2 style="color:green">Online enemies</h2>

<?php
foreach ($arr as $char) {
    if($char->rank>=1){
            echo "<a style=\"color:green\" href=\"http://www.tibia.com/community/?subtopic=characters&name=$char->name\">";
            echo $char->name." ";
            echo "</a>";
            echo level($char->name)."<b> ";
            echo vocation($char->name)."</b> (<i>";
            echo $char->desc." </i>)<br />";
    }
}
?>
<br /> 
<h2 style="color:red">Offline enemies</h2>
<?php
foreach ($arr as $char) {
    if($char->rank==0){
            echo "<a style=\"color:red\" href=\"http://www.tibia.com/community/?subtopic=characters&name=$char->name\">";
            echo $char->name." ";
            echo "</a>";
            echo level($char->name)."<b> ";
            echo vocation($char->name)."</b> (<i>";
            echo $char->desc." </i>)<br />";
    }
}

?>

并且你的check_online()函数看起来像这样:

function check_online($name,$content){
    $count=substr_count($name, " "); 
    if($count > 0){ $ex=explode(" ",$name); $namez=$ex[1]; $nameused=$namez; } 
    else{ $nameused=$name; } 
    if(preg_match("/$nameused/",$content)){ $status="online"; } 
    else{ $status="offline"; }     
    return $status; 
}

您还可以执行以下操作以加快速度

  1. 停止使用在innodb上非常糟糕的select *
  2. 在数据库上放置更好的索引以使记录集返回更快
  3. 安装PHP 5.4,因为它更快,尤其是在每次迭代中创建新对象时
  4. 使用字节码加速器/缓存,例如xdebug

答案 2 :(得分:0)

您应该避免在SQL查询中使用distinct(*)关键字 有关更多信息,请阅读此http://blog.sqlauthority.com/category/sql-coding-standards/page/2/