Php code implementation efficiency

Source: Internet
Author: User
Php code execution efficiency here we will talk about code execution. Although php is very fast, it is still meat for mosquitoes, though it is small ~? The following is the execution time of some codes .? // The following program execution times $ _ I = 1000000 ;? Next let's take a look at the for loop? /** Php code execution efficiency

Here we will talk about the execution of some code. Although php is executed very quickly, it is still meat if the mosquito is small ~

?

The following is the execution time of some code.

?

// The following program executions $ _ I = 1000000;
?

Next let's take a look at the for loop.

?

/*** This is the loop and judgment frequently written in the past * for (I = 0; I <5; I ++) * if (0 <1) */$ _ val = 1; for ($ _ j = 0; $ _ j <$ _ I; $ _ j ++) {if (0 <$ _ val) {}}/*** execution time * 0.106183052063*0.105647087097*0.105874061584*0.117154121399 * // *** Next let's take a look at the changed */$ _ val = 1; for ($ _ I; $ _ I --) {if (0 <$ _ val) {}}/*** execution time * 0.0908319950104*0.0884439945221*0.0853419303894*0.0844340324402 */

? It is also a loop ($ _ I), but the following is less than 0.01 ~ 0.02 seconds. The reason is that you do not need to judge $ _ I every time, because when $ _ I = 0, the value is false.

?

Therefore, we can write the if statement in this way.

$_val = 1;if ($_val) {}

? If the number is true, 0 is false.


The following is a very easy mistake. declare variables in the loop. let's look at the execution time.

$ _ Val = 1; for ($ _ I; $ _ I --) {if ($ _ val) {}}/*** execution time * 0.100801944733*0.0974791049957*0.0993800163269*0.112987041473 * // put the variable in a loop for ($ _ I; $ _ I --) {$ _ val = 1; if ($ _ val) {}}/*** execution time * 0.133583068848*0.117377996445*0.116209983826*0.12203502655 */

? The above code shows the gap.


Judgment or situation

$ _ Val = 3; for ($ _ I; $ _ I --) {if (3 === _ val | 4 === _ val | 5 === _ val) {}}/*** execution time * 0.11983704567*0.12117600441*0.129379987717*0.131067991257 */$ _ val = 5; for ($ _ I; $ _ I --) {if (3 === _ val | 4 === _ val | 5 === _ val) {}}/*** execution time * 0.27806186676*0.316290855408*0.259559869766*0.277565956116 * // use the php built-in function $ _ val = 5; for ($ _ I; $ _ I; $ _ I --) {if (in_array ($ _ val, array (1.13307905197, 5) {}}/*** 1.12578582764*1.14648389816 */

? Use php functions if you have php functions. you will find that php functions are easy to use.


Another query

// For example, when querying statistics, check whether the selected id FROM post WHRE user_id = '{$ _ user_id}' AND ...... // if this happens, it means the efficiency is very slow. In fact, it is to check whether there is such a data volume, you can add a LIMIT parameter LIMIT 1 SELECT id FROM post WHRE user_id = '{$ _ user_id}' AND ...... LIMIT 1
?

I hope the above is helpful to everyone.

?

The first example of liuzhiqiangruc is faster because it is not necessary to compare the last two. The problem with in_array is that every loop needs to construct an array (3, 4, 5), which is quite troublesome to construct an array. Why do we need to construct an array in the loop? isn't it faster to construct an array out of the loop?


In addition, in the last query example, adding or not adding limit 1 depends on the business needs. if you only need to find a record, it is certainly OK. On the second floor, wangyangtoy 2011-07-06 liuzhiqiangruc wrote that the first example of brother or is faster because it is not necessary to compare the last two. The problem with in_array is that every loop needs to construct an array (3, 4, 5), which is quite troublesome to construct an array. Why do we need to construct an array in the loop? isn't it faster to construct an array out of the loop?


In addition, in the last query example, adding or not adding limit 1 depends on the business needs. if you only need to find a record, it is certainly OK.

In my example, it means that if "|" is used, it depends on luck, while in_array is used at the same speed ~
You are talking about constructing an array (3, 4, 5). you are right. you can take it out of the loop. I have explained it above.

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.