技术头条 - 一个快速在微博传播文章的方式     搜索本站
您现在的位置首页 --> 算法 --> 三元式(ternary)性能优化

三元式(ternary)性能优化

浏览:798次  出处信息

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

我们都知道PHP用写时复制来对变量复制做性能优化, 而在以前的三元式中, 却每次都会复制, 这在操作数是大数组的情况下, 会造成性能问题:

  1. <?php
  2. $a = range(1, 1000);
  3. $i = 0;
  4.  
  5. $start = microtime(true);
  6. while (++$i < 1000) {
  7.     $b = isset($a)? $a : NULL;
  8. }
  9.  
  10. var_dump(microtime(true) - $start);

相比, 我们采用if-else来做同样的功能:

  1. <?php
  2. $a = range(1, 1000);
  3. $i = 0;
  4.  
  5. $start = microtime(true);
  6. while (++$i < 1000) {
  7.     if (isset($a)) {
  8.         $b = $a;
  9.     } else {
  10.         $b = NULL;
  11.     }
  12. }
  13. var_dump(microtime(true) - $start);

前者在我的机器上, 运行时间为: float(0.0448620319366), 而采用if-else则是: float(0.000280006027222)

为此, Arnaud提供了一个patch, 来对三元式做了一个优化, 使得三元式不会每次都复制操作数, 在优化以后, 开头给的例子的运行时间降低为: float(0.00029182434082031)

The ternary operator always copies its second or third operand, which is very
slow compared to an if/else when the operand is an array for example:

$a = range(0,9);

// this takes 0.3 seconds here:

for ($i = 0; $i < 5000000; ++$i) {
if (true) {
$b = $a;
} else {
$b = $a;
}
}

// this takes 3.8 seconds:

for ($i = 0; $i < 5000000; ++$i) {
$b = true ? $a : $a;
}

I've tried to reduce the performance hit by avoiding the copy when possible
(patch attached).

Benchmark:

Without patch: (the numbers are the time taken to run the code a certain
amount of times)

$int = 0;
$ary = array(1,2,3,4,5,6,7,8,9);

true ? 1 : 0 0.124
true ? 1+0 : 0 0.109
true ? $ary : 0 2.020 !
true ? $int : 0 0.103
true ? ${'ary'} : 0 2.290 !
true ?: 0 0.091
1+0 ?: 0 0.086
$ary ?: 0 2.151 !
${'var'} ?: 0 2.317 !

With patch:

true ? 1 : 0 0.124
true ? 1+0 : 0 0.195
true ? $ary : 0 0.103
true ? $int : 0 0.089
true ? ${'ary'} : 0 0.103
true ?: 0 0.086
1+0 ?: 0 0.159
$cv ?: 0 0.090
${'var'} ?: 0 0.089

The array copying overhead is eliminated. There is however a slowdown in some
of the cases, but overall there is no completely unexpected performance hit as
it is the case currently.

不过, 还是要提醒下: PHP 5.4还处于开发阶段, 在最终release之前, 任何新特性都可能被调整或者更改. 如果大家有任何建议, 也欢迎反馈, 帮助我们使得PHP变得更好.

QQ技术交流群:445447336,欢迎加入!
扫一扫订阅我的微信号:IT技术博客大学习
© 2009 - 2024 by blogread.cn 微博:@IT技术博客大学习

京ICP备15002552号-1