|
| ||||||||||
| Tags: array, code optimization, loop, php, ram, script, template |
![]() |
| | Thread Tools | Search this Thread |
|
#1
| |||
| |||
| Some tricks of PHP code optimization
We'll start by optimizing the few milliseconds you might lose the use of office less effective than others. All benchs that follow were performed with version 5.2.4 of PHP, the latest as I write these lines. They consist in the execution of a loop containing a number n of iterations (this number n is specified in front of each bench because the differences are sometimes so small that they require a larger number of iterations so that the they can be seen). Within this loop is the code for which we test performance. At the end of the script, we count the number of seconds required to run and therefore we compare the different codes between them. The benchs were executed five times to take the average generation time and reduce errors. Attack!! |
|
#2
| |||
| |||
| Re: Some tricks of PHP code optimization The single and double quotes: Let's start with a classic PHP namely single and double quotes (quotation marks). They derive their main difference is that everything is within single quotes is not interpreted, contrary to what is inside double quotes. In practice the following code does exactly the same thing: PHP Code: Version 1: PHP Code: PHP Code: Version 1: PHP Code: PHP Code: PHP Code: |
|
#3
| |||
| |||
| Re: Some tricks of PHP code optimization
It is noted here that there is no average difference between print and echo (attention, however, I found that print could be faster than echo, but it's usually the opposite. The average has smoothed results) . You can (and only echo) replace the item with a comma concatenation. This can be faster (about 200 iterations I did not get enough differences, however). This is because it is often faster to concatenate a piece of string and throw everything in the same buffer as close and reopen the buffer needed each time you use echo. Here is what it is: Concatenation with the point: PHP Code: PHP Code:
Version 1: PHP Code: PHP Code: PHP Code: |
|
#4
| |||
| |||
| Re: Some tricks of PHP code optimization
Turning now to the loops, and more specifically to the use of the count () function in a loop. Many of you put a count () in a loop so that the loop stops once to increment the variable exceeds the value returned by count (). However, in general, you work on an array of fixed size, ie count () will return to the same value each time. When you make a loop, the value of count () is recalculated for nothing! Here are two codes which I mean: Version 1: PHP Code: PHP Code: Version 1: PHP Code: PHP Code:
|
|
#5
| |||
| |||
| Re: Some tricks of PHP code optimization
Prefer the use of the function foreach () instead of using the couple while (list () = each ()). We'll prove it to fill an array of 1000 random values, then we'll walk 200 times this picture of two ways that I just mentioned. Here are the codes used: Version 1: PHP Code: PHP Code: |
![]() |
|
| Thread Tools | Search this Thread |
| |
Similar Threads for: "Some tricks of PHP code optimization" | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Better code with onsite optimization | Inigo | Technology & Internet | 6 | 23-02-2011 03:02 PM |
| Optimization in MySQL | Calan | Software Development | 4 | 21-12-2010 12:35 AM |
| SSE Optimization on AMD CPU | Tobius | Software Development | 2 | 14-01-2009 06:54 PM |
| Oracle optimization HELP!!! | spectre | Tips & Tweaks | 1 | 21-06-2008 04:13 PM |
| System Optimization Tricks | ravi_mishra | Tips & Tweaks | 22 | 20-05-2006 12:23 PM |