找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 1261|回复: 0

[教程] PHP 5.4 由Arnaud 引入了一个对三元式的优化方案.

[复制链接]
发表于 2013-4-11 11:16:34 | 显示全部楼层 |阅读模式 来自 广东省湛江市
  1. 我们都知道PHP用写时复制来对变量复制做性能优化, 而在以前的三元式中, 却每次都会复制, 这在操作数是大数组的情况下, 会造成性能问题:
  2.            <?php
  3.          $a = range(1, 1000);
  4.          $i = 0;
  5.          
  6.         $start = microtime(true);
  7.          while (++$i < 1000) {
  8.          $b = isset($a)? $a : NULL;
  9.          }
  10.          
  11.         var_dump(microtime(true) - $start);
  12.          相比, 我们采用if-else来做同样的功能:
  13.            <?php
  14.          $a = range(1, 1000);
  15.          $i = 0;
  16.          
  17.         $start = microtime(true);
  18.          while (++$i < 1000) {
  19.          if (isset($a)) {
  20.          $b = $a;
  21.          } else {
  22.          $b = NULL;
  23.          }
  24.          }
  25.          var_dump(microtime(true) - $start);
  26.          前者在我的机器上, 运行时间为: float(0.0448620319366), 而采用if-else则是: float(0.000280006027222)
  27.            为此, Arnaud提供了一个patch, 来对三元式做了一个优化, 使得三元式不会每次都复制操作数, 在优化以后, 开头给的例子的运行时间降低为: float(0.00029182434082031)
  28.            The ternary operator always copies its second or third operand, which is very
  29.          slow compared to an if/else when the operand is an array for example:
  30.            $a = range(0,9);
  31.            // this takes 0.3 seconds here:
  32.            for ($i = 0; $i < 5000000; ++$i) {
  33.          if (true) {
  34.          $b = $a;
  35.          } else {
  36.          $b = $a;
  37.          }
  38.          }
  39.            // this takes 3.8 seconds:
  40.            for ($i = 0; $i < 5000000; ++$i) {
  41.          $b = true ? $a : $a;
  42.          }
  43.            I've tried to reduce the performance hit by avoiding the copy when possible
  44.          (patch attached).
  45.            Benchmark:
  46.            Without patch: (the numbers are the time taken to run the code a certain
  47.          amount of times)
  48.            $int = 0;
  49.          $ary = array(1,2,3,4,5,6,7,8,9);
  50.            true ? 1 : 0 0.124
  51.          true ? 1+0 : 0 0.109
  52.          true ? $ary : 0 2.020 !
  53.          true ? $int : 0 0.103
  54.          true ? ${'ary'} : 0 2.290 !
  55.          true ?: 0 0.091
  56.          1+0 ?: 0 0.086
  57.          $ary ?: 0 2.151 !
  58.          ${'var'} ?: 0 2.317 !
  59.            With patch:
  60.            true ? 1 : 0 0.124
  61.          true ? 1+0 : 0 0.195
  62.          true ? $ary : 0 0.103
  63.          true ? $int : 0 0.089
  64.          true ? ${'ary'} : 0 0.103
  65.          true ?: 0 0.086
  66.          1+0 ?: 0 0.159
  67.          $cv ?: 0 0.090
  68.          ${'var'} ?: 0 0.089
  69.            The array copying overhead is eliminated. There is however a slowdown in some
  70.          of the cases, but overall there is no completely unexpected performance hit as
  71.          it is the case currently.
  72.            不过, 还是要提醒下: PHP 5.4还处于开发阶段, 在最终release之前, 任**特性都可能被调整或者更改. 如果大家有任何建议, 也欢迎反馈, 帮助我们使得PHP变得更好.
  73.            谢谢
复制代码

评分

参与人数 1金币 +1 收起 理由
猫性男孩 + 1 智者的提问,再接再厉

查看全部评分

发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;

如何回报帮助你解决问题的坛友,好办法就是点击帖子下方的评分按钮给对方加【金币】不会扣除自己的积分,做一个热心并受欢迎的人!

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则 需要先绑定手机号

关闭

站长推荐上一条 /1 下一条

QQ|侵权投诉|广告报价|手机版|小黑屋|西部数码代理|飘仙建站论坛 ( 豫ICP备2022021143号-1 )

GMT+8, 2024-6-6 05:03 , Processed in 1.033126 second(s), 13 queries , Redis On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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