PHP 5.4 introduced a three-dimensional optimization scheme from Arnaud.

Source: Internet
Author: User
Tags array copy new features range

We all know that PHP uses write-time replication to perform performance optimizations on variable replication, but in the previous ternary, it replicates every time, which can cause performance problems when the operands are large arrays:

<?php
$a = range (1, 1000);
$i = 0;

$start = Microtime (true);
while (+ + $i < 1000) {
$b = Isset ($a)? $a: NULL;
}

Var_dump (Microtime (True)-$start);
We use If-else to do the same function:

<?php
$a = range (1, 1000);
$i = 0;

$start = Microtime (true);
while (+ + $i < 1000) {
if (Isset ($a)) {
$b = $a;
} else {
$b = NULL;
}
}
Var_dump (Microtime (True)-$start);
The former on my machine, the operating time is: float (0.0448620319366), and the use of If-else is: float (0.000280006027222)

To this end, Arnaud provides a patch, to the three-dimensional to do an optimization, so that the ternary does not copy the operands each time, after optimization, the beginning of the example of the running time reduced to: float (0.00029182434082031)

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

$a = range (0,9);

This is 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.

However, it is also a reminder that PHP 5.4 is still in the development phase and that any new features may be adjusted or changed before the final release. If you have any suggestions, also welcome feedback to help us make PHP better.

Thank you







Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.