设为首页 收藏本站
查看: 689|回复: 0

[经验分享] PHP performance tips

[复制链接]

尚未签到

发表于 2017-3-4 12:53:48 | 显示全部楼层 |阅读模式
PHP performance tips
  Author:  Eric Higgins, Google Webmaster
  Recommended experience:  Beginner to intermediate PHP knowledge
  PHP
is a very popular scripting language, used on many popular sites across
the web. In this article, we hope to help you to improve the
performance of your PHP scripts with some changes that you can make
very quickly and painlessly. Please keep in mind that your own
performance gains may vary greatly, depending on which version of PHP
you are running, your web server environment, and the complexity of
your code.

Profile your code to pinpoint bottlenecks
  Hoare's dictum states that Premature optimization is the root of all evil
,
an important thing to keep in mind when trying to make your web sites
faster. Before changing your code, you'll need to determine what is
causing it to be slow. You may go through this guide, and many others
on optimizing PHP, when the issue might instead be database-related or
network-related. By profiling your PHP code
, you can try to pinpoint bottlenecks.

Upgrade your version of PHP
  The
team of developers who maintain the PHP engine have made a number of
significant performance improvements over the years. If your web server
is still running an older version, such as PHP 3 or PHP 4, you may want
to investigate upgrading before you try to optimize your code.



  • Migrating from PHP 4 to PHP 5.0.x

  • Migrating from PHP 5.0.x to PHP 5.1.x

  • Migrating from PHP 5.1.x to PHP 5.2.x

Use caching
  Making use of a caching module, such as Memcache
, or a templating system which supports caching, such as Smarty
, can help to improve the performance of your website by caching database results and rendered pages.

Use output buffering
  PHP
uses
a memory buffer to store all of the data that your script tries to
print. This buffer can make your pages seem slow, because your users
have to wait for the buffer to fill up before it sends them any data.
Fortunately, you can make some changes that will force PHP to flush the
output buffers sooner, and more often, making your site feel faster to
your users.



  • Output Buffering Control

Avoid writing naive setters and getters
  When
writing classes in PHP, you can save time and speed up your scripts by
working with object properties directly, rather than writing naive
setters and getters. In the following example, the dog
class uses the setName()
and getName()
methods for accessing the name
property.

class
dog
{

 
public
$name
=

''
;

 
public

function
setName
(
$name
)

{

    $this
->
name
=
$name
;

 
}

 
public

function
getName
()

{

   
return
$this
->
name
;

 
}


}

  Notice that setName()
and getName()
do nothing more than store and return the name
property, respectively.

$rover
=

new
dog
();

$rover
->
setName
(
'rover'
);

echo $rover
->
getName
();

  Setting and calling the name
property directly can run up to 100% faster
, as well as cutting down on development time.

$rover
=

new
dog
();

$rover
->
name
=

'rover'
;

echo $rover
->
name
;

Don't copy variables for no reason
  Sometimes
PHP
novices attempt to make their code "cleaner" by copying predefined
variables to variables with shorter names before working with them.
What this actually results in is doubled memory consumption (when the
variable is altered), and therefore, slow scripts. In the following
example, if a user had inserted 512KB worth of characters into a
textarea field. This implementation would result in nearly 1MB of
memory being used.

$description = strip_tags($_POST['description']);
echo $description;
  There's no reason to copy the variable above. You can simply do this operation inline and avoid the extra memory consumption:

echo strip_tags($_POST['description']);
Avoid doing SQL queries within a loop
  A common mistake is placing a SQL query inside of a loop. This results in
multiple round trips to the database, and significantly slower scripts.
In the example below, you can change the loop to build a single SQL
query and insert all of your users at once.

foreach ($userList as $user) {
$query = 'INSERT INTO users (first_name,last_name) VALUES("' . $user['first_name'] . '", "' . $user['last_name'] . '")';
mysql_query($query);
}
  Produces:

INSERT INTO users (first_name,last_name) VALUES("John", "Doe")
  Instead of using a loop, you can combine the data into a single database query.

$userData = array();
foreach ($userList as $user) {
  $userData[] = '("' . $user['first_name'] . '", "' . $user['last_name'] . '")';
}
$query = 'INSERT INTO users (first_name,last_name) VALUES' . implode(',', $userData);
mysql_query($query);
  Produces:

INSERT INTO users (first_name,last_name) VALUES("John", "Doe"),("Jane", "Doe")...

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-350225-1-1.html 上篇帖子: thrift php客户端编写 下篇帖子: PHP 之数组
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表