PHP5.4 Arnaud introduced a three-element optimization solution.

Source: Internet
Author: User
We all know that PHP uses write-time replication to optimize the performance of variable replication, but in the previous three-element formula, it will replicate every time. This is when the operand is a large array, will cause performance problems: & lt ;? Php $ a = range (1,1000); $ I = 0; $ start = microtime (true); wh... "> <LINKhref =" http: // www.

 

We all know that PHP uses write-time replication to optimize the performance of variable replication, but in the previous three-element formula, it will replicate every time. This is when the operand is a large array, this may cause performance problems:

$ A = range (1, 1000 );
$ I = 0;

$ Start = microtime (true );
While (++ $ I <1000 ){
$ B = isset ($ )? $ A: NULL;
}

Var_dump (microtime (true)-$ start );
In comparison, we use if-else for the same function:

$ A = range (1, 1000 );
$ I = 0;

$ Start = microtime (true );
While (++ $ I <1000 ){
If (isset ($ )){
$ B = $;
} Else {
$ B = NULL;
}
}
Var_dump (microtime (true)-$ start );
The former runs on my machine at: float (0.0448620319366), and if-else is: float (0.000280006027222)

To this end, Arnaud provides a patch to optimize the ternary formula so that the ternary formula does not copy the operands every time. After optimization, the running time of the example given at the beginning is reduced: 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 = $;
} Else {
$ B = $;
}
}

// This takes 3.8 seconds:

For ($ I = 0; $ I <5000000; ++ $ I ){
$ B = true? $ 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
It is the case currently.

However, we still need to remind you that PHP 5.4 is still in the development stage. before the final release, any new features may be adjusted or changed. if you have any suggestions, please feel free to give us feedback and 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.