有哪些好的 PHP 性能技巧? [英] What are some good PHP performance tips?

查看:32
本文介绍了有哪些好的 PHP 性能技巧?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我听说过一些 PHP 的性能技巧,例如使用 strtr() 而不是 str_replace() 而不是 preg_replace(),具体取决于情况.

I've heard of some performance tips for PHP such as using strtr() over str_replace() over preg_replace() depending on the situation.

就使用某些功能而不是其他功能和代码风格而言,您知道哪些性能提示?

As far as using certain functions over others, and code style, what are some of the performance tips that you know of?

我不是在谈论使用那些降低代码可读性的东西,比如 !isset($foo{5}) 而不是 strlen($foo) <5,我说的是使用 preg_ 函数而不是 ereg_ 函数作为正则表达式.

I'm not talking about use of things that make code less readable, like !isset($foo{5}) over strlen($foo) < 5, I'm talking about things like using preg_ functions over ereg_ functions for regex.

我问这个的原因不是为了挑剔什么时候优化,而是为了大致了解在有限的一组替代方案中什么往往是最有效的.例如,检查 mysql 语句是否返回错误可以说是比抑制错误更好的做法.

The reason I ask this is not for nitpicking over when to optimize, but to get a general idea of what tends to be most efficient in a limited set of alternatives. For instance, checking if a mysql statement returned an error is arguably better practice than suppressing the errors to begin with.

推荐答案

这个问题真的很模糊.当你想优化你的脚本时,你首先检查你的数据库并尝试优化你的算法.没有多少纯粹的 PHP 性能技巧会很重要.让我们看看:

This question is really vague. When you want to optimize your script, you first check your database and try to optimize your algorithms. There aren't many pure PHP performance tips that are going to matter. Let's see :

  • 连接变量比将它们放在双引号字符串中要快.

  • Concatening variables is faster than just putting them in a double-quotation mark string.

$var = 'Hello ' . $world; // is faster than
$var = "Hello $world"; // or
$var = "Hello {$world}";

是的,它更快,但第二种和第三种形式更具可读性,速度损失如此之低,甚至无关紧要.

Yes, it's faster, but the second and third form are even more readable and the loss of speed is so low it doesn't even matter.

  • 使用循环时,如果您的条件使用常量,请将其放在循环之前.例如:

  • When using a loop, if your condition uses a constant, put it before the loop. For instance :

for ($i = 0; $i < count($my_array); $i++)

这将每次计算 count($my_array).只需在循环之前,甚至在循环内部创建一个额外的变量:

This will evaluate count($my_array) every time. Just make an extra variable before the loop, or even inside :

for ($i = 0, $count = count($my_array); $i < $count; $i++)

  • 最糟糕的事情肯定是在循环内查询.要么是因为缺乏知识(试图在 PHP 中模拟 JOIN),要么只是因为您没有考虑它(例如许多插入到循环中).

    • The worst thing is definitely queries inside loops. Either because of lack of knowledge (trying to simulate a JOIN in PHP) or just because you don't think about it (many insert into in a loop for instance).

      $query = mysql_query("SELECT id FROM your_table");
      while ($row = mysql_fetch_assoc($query)) {
          $query2 = mysql_query("SELECT * FROM your_other_table WHERE id = {$row['id']}");
          // etc
      }
      

    • 切勿这样做.这是一个简单的 INNER JOIN.

      Never do this. That's a simple INNER JOIN.

      可能还有更多,但实际上,不值得全部写下来.编写代码,稍后优化.

      There are probably more, but really, it's not worth writing all of them down. Write your code, optimize later.

      附言我在没有答案时开始写这个答案,链接中可能已经说了一些东西.

      P.S. I started writing this answer when there was none, there may be some things already said in links.

      出于某种原因,我无法正确格式化代码.我真的不明白为什么.

      for some reason, I can't format the code correctly. I really don't understand why.

      这篇关于有哪些好的 PHP 性能技巧?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆