简体   繁体   English

PHP脚本性能问题

[英]PHP script performance issue

There seems to be a performance issue with the script as it is really slow. 脚本似乎存在性能问题,因为它非常慢。 I was wondering what I could do to speed this up. 我想知道我能做些什么才能加快速度。 If you have any ideas, please let me know. 如果您有任何想法,请告诉我。 I can't seem to figure it out. 我似乎无法弄明白。

Below is the code: 以下是代码:

<?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";
}
?>

Thank you. 谢谢。 Jae

  1. Replace every string concatenation with the "." 用“。”替换每个字符串连接。 operator with an array push, for example array [ ] = "foo" and then return a string concatenation implode ( array ); 具有数组推送的运算符,例如array [] =“foo”然后返回字符串连接implode(数组);
  2. Use ob_start(); 使用ob_start(); to cache the output: 缓存输出:

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

  3. You can optimize if to switch and change the order to your expected result. 您可以优化是否切换并将订单更改为预期结果。 For example if result a is more likely then result b the condition to catch result a should be the first condition. 例如,如果结果a更可能那么结果b捕获结果的条件应该是第一个条件。

  4. Put a primary key and secondary key on your table(s). 在表上放置主键和辅助键。

1.1) Don't use a glue and don't add the construction of the array to the time. 1.1)不要使用胶水,也不要添加阵列的结构。 Here is a benchmark for http://www.sitecrafting.com/blog/php-string-concat-vs-array 1.2) http://dan.doezema.com/2011/07/php-output-profiling-echo-vs-concat ( although echo is fastest concat is slower then array and also he uses a glue! 1.3) https://stackoverflow.com/questions 1.4) http://www.sitepoint.com/high-performance-string-concatenation-in-php/ 以下是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

Conclusion: use implode with glue. 结论:使用胶水内爆。

Your toHtml() is pointless (not to mention it's implemented poorly hence the low performance), you don't need to convert every unicode character to &#...; 你的toHtml()是没有意义的(更不用说它实现得很差,因此性能很低),你不需要将每个unicode字符转换为&#...; notation, just put this in your <head> 记法,只需把它放在你的<head>

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

and print utf-8 strings as they are, your browser will know how to deal with them. 并按原样打印utf-8字符串,您的浏览器将知道如何处理它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM