PHP脚本性能问题

时间:2011-08-01 09:17:05

标签: php performance

脚本似乎存在性能问题,因为它非常慢。我想知道我能做些什么才能加快速度。如果您有任何想法,请告诉我。我似乎无法弄明白。

以下是代码:

<?php
include_once("connect.php.inc");
class HtmlEnc{ 
  static function uniord($c) { 
    $ud = 0; 
    if (ord($c{0}) >= 0 && ord($c{0}) <= 127) $ud = ord($c{0}); 
    if (ord($c{0}) >= 192 && ord($c{0}) <= 223) $ud = (ord($c{0})-192)*64 + (ord($c{1})-128); 
    if (ord($c{0}) >= 224 && ord($c{0}) <= 239) $ud = (ord($c{0})-224)*4096 + (ord($c{1})-128)*64 + (ord($c{2})-128); 
    if (ord($c{0}) >= 240 && ord($c{0}) <= 247) $ud = (ord($c{0})-240)*262144 + (ord($c{1})-128)*4096 + (ord($c{2})-128)*64 + (ord($c{3})-128); 
    if (ord($c{0}) >= 248 && ord($c{0}) <= 251) $ud = (ord($c{0})-248)*16777216 + (ord($c{1})-128)*262144 + (ord($c{2})-128)*4096 + (ord($c{3})-128)*64 + (ord($c{4})-128); 
    if (ord($c{0}) >= 252 && ord($c{0}) <= 253) $ud = (ord($c{0})-252)*1073741824 + (ord($c{1})-128)*16777216 + (ord($c{2})-128)*262144 + (ord($c{3})-128)*4096 + (ord($c{4})-128)*64 + (ord($c{5})-128); 
    if (ord($c{0}) >= 254 && ord($c{0}) <= 255) $ud = false; // error 
    return $ud; 
  } 

  static function toHtml($str){ 
      $html_str = ""; 
      while (strlen($str) > 0) { 
        preg_match("/^(.)(.*)$/u", $str, $match); 
        $test = utf8_decode($match[1]); 
        if ($test != "?") { 
          $html_str .= htmlentities(htmlentities($test)); 
        } else if (strlen($match[1]) > 1) { 
          $html_str .= "&amp;#".self::uniord($match[1]).";"; 
        } else $html_str .= htmlentities(htmlentities($match[1])); 
        $str = $match[2]; 
      } 
      return $html_str; 
  } 
} 

/*
List of mail servers
*/

function alreadyDone($domain){
    $domain = strtolower($domain);
    $qry = "SELECT * FROM emdb WHERE domain ='" . $domain . "'";
    $result = mysql_query($qry);
    return (mysql_num_rows($result)!=0);
}

$template_fn = $_REQUEST['template']; 
//"mail_template.html";
$keywords = HtmlEnc::toHtml($_REQUEST['Keywords']);
$keywords = str_replace("&amp;","&",$keywords);

$domain = $_REQUEST['Domain'];
$rank = $_REQUEST['Rank'];
$to = $_REQUEST['Email'];
$adminEmail = "test@example.com";


if (!alreadyDone($domain)) {

    if ($to=="") {
        $to = "info@" . $domain;
    }

    function int_divide($x, $y) {
    if ($x == 0) return 0;
    if ($y == 0) return FALSE;
    return ($x - ($x % $y)) / $y;
    }

    $page = int_divide($rank,10) + 1;

if ($template_fn == "mail_template_nick.html" || $template_fn == "mail_template_chet.html" || "mail_template_salesperson.php") 
    $subject = $domain." is on Page ".$page." of Google - Want to be #1?";
elseif ($template_fn == "seo_template.html")
    $subject = "Outsource your SEO - Lowest rates guaranteed!";
elseif ($template_fn == "adwords_template.html")
    $subject = $domain . " - Save your money on Google Adwords";
else $subject = $domain . " is ranked " . $rank . " on Google - Be 1st!";

    $message = file_get_contents($template_fn);
    /*$message = "<body>
            <p>Hi There,</p>
            <p>How's your week been so far?</p>
            <p>When I Googled &quot;{KEYWORD}&quot;, I found {WEBSITE} on  page {PAGE}, not on page 1. This means consumers will find your competitors  before they find you!</p>
            <p>93% of all people, never go past the 1st page of Google, so  at this very moment you're losing sales &amp; leads to a competitor. </p>
            <p>If you agree your Google exposure needs drastic improvement,  please call me for a chat, I'm sure I can give some good, free advice. </p>
            <p>&nbsp;</p>
            <p><strong>Best Regards,</strong></p>
            <p><strong>Kayne Chong </strong><strong>- Business Development Director</strong></p>
            <p><strong>Tel:</strong>&nbsp; | &nbsp;<strong>Fax:&nbsp;</strong><br />
              <strong>Office:</strong>&nbsp;<br />
              <strong>Web:</strong>&nbsp;<a href='http://www.seoagency.com.sg/' target='_blank'><em>www.seoagency.com.sg</em></a><br />
              <strong><em>Web  marketing that brings BUSINESS to you!</em></strong></p>
            </body>";*/
    $message = str_replace("{WEBSITE}", $domain , $message);

    $message = str_replace("{PAGE}", $page , $message);
    //$message = str_replace("{RANK}", $rank , $message);

    $message = str_replace("{KEYWORD}", $keywords , $message);

    $headers  = 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

    // Additional headers
    /*$headers .= 'Bcc: ' . $adminEmail . "\r\n";
if ($template_fn == "mail_template_salesperson.php")
{   $headers .= 'From: Kayne - Web Marketing Experts <test@example.com>' . "\r\n";
    $headers .= 'Reply-To: test@example.com' . "\r\n";}
elseif ($template_fn == "mail_template_chet.html")
{   $headers .= 'From: Chester - Web Marketing Experts <test@example.com>' . "\r\n";
    $headers .= 'Reply-To: test@example.com' . "\r\n";}*/

    $headers  = 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
    $headers .= 'Bcc: ' . $adminEmail . "\r\n";
    $headers .= 'From: Info - Web Marketing Experts <test@example.com>' . "\r\n";
    $headers .= 'Reply-To:  test@example.com' . "\r\n";



    if (mail($to, $subject, $message, $headers)) {
        echo "Mail successfully sent to $to and $adminEmail";
    } else echo "Mail sending failed!";

    $qry = "INSERT INTO emdb (domain, keywords, rank, last, count) VALUES ('$domain','$keywords','$rank',CURDATE(), '1')";
    mysql_query($qry) or die(mysql_error());

    echo "<BR />";
    echo "DB updated";
} else {
  echo "Domain name $domain has already been processed";
}
?>

谢谢。 宰

2 个答案:

答案 0 :(得分:2)

  1. 将每个字符串连接替换为“。”具有数组push的运算符,例如array [] =“foo”然后返回字符串连接implode(array);
  2. 使用ob_start();缓存输出:

    ob_start();   echo $ a,$ b,$ c;   $ str = ob_get_contents();   ob_end_clean();

  3. 您可以优化if切换并将订单更改为预期结果。例如,如果结果a更可能那么结果b捕获结果的条件应该是第一个条件。

  4. 在桌面上放置主键和辅助键。
  5. 1.1)不要使用胶水,也不要添加阵列的结构。这是http://www.sitecrafting.com/blog/php-string-concat-vs-array的基准 1.2)http://dan.doezema.com/2011/07/php-output-profiling-echo-vs-concat(虽然echo是最快的concat比数组慢,而且他还使用胶水! 1.3)https://stackoverflow.com/questions 1.4)http://www.sitepoint.com/high-performance-string-concatenation-in-php/

     Here are my results (30000 strings, time in milliseconds) (Script is taken from 1.4):
    
     standard:         0.02418089
     implode w/ glue:  0.00435901
     implode w/o glue: 0.02205801
     foreach:          0.02081609
    

    结论:使用胶水内爆。

答案 1 :(得分:2)

你的toHtml()是没有意义的(更不用说它实现得很差,因此性能很低),你不需要将每个unicode字符转换为&amp;#...;表示法,只需将其放入<head>

即可
<meta http-equiv="content-type" content="text/html; charset=utf-8" />

并按原样打印utf-8字符串,您的浏览器将知道如何处理它们。