PHP Multiple optimization methods

Source: Internet
Author: User
Tags arrays data structures lowercase php script phpinfo strlen switch case zend

PHP Multiple optimization methods

One of the PHP optimization configurations lets the forum speed faster PHP acceleration set PHP Acceleration: Zend Optimizer Optimize PHP program

Zend Optimizer V2.5.7 for Windows
Software language: English
Operating Environment: WIN9X/NT/2000/XP

Zend Optimizer V2.5.7 for Windows software Introduction:

Zend Optimizer uses optimized code to improve the execution speed of PHP 4.0 applications. The principle of implementation is to optimize the code that is generated by running the compiler (Run-time Compiler) before it is finally executed. In general, PHP programs that use Zend Optimizer are 40% to 100% faster than those that are not used. This means that visitors to the site can browse the Web more quickly, thus accomplishing more business and creating better customer satisfaction. Faster response also means that hardware investment can be saved and the services provided by the site are enhanced. Therefore, the use of Zend Optimizer, is tantamount to improving the profitability of e-commerce. Zend Optimizer can bring a lot of benefits to PHP users, especially those who run the site. Running a PHP program quickly can significantly reduce the CPU load on the server and reduce the response time by half, which is the time between when the visitor clicks on the link to the server and starts reading the page.

Installing Zend Optimizer optimizing PHP programs

Zend Optimizer is a free PHP optimization software run by the PHP core engine "Zend" Http://www.zend.com founder Zend technology company. According to Zend company revealed that the use of this software in some cases can at least improve performance of more than 30%! Now let's take a look at how to configure this set of software.

The installation of Zend optimizer is simple

1, run the installation files, first prompts you to choose the installation directory, select a directory you like, click "Next".
2, the Installation Wizard will ask you to select the current Web server (Apache, IIS or other), select a good server and click "Next"
3, the Installation Wizard prompts you to confirm the location of the php.ini (default is c:windows) click "Next" to Prompt "backup php.ini to C:windowsphp.ini-optimizer-bak, When you uninstall the software you can revert to the original php.ini) click "Next".
4. The Setup Wizard prompts you to restart the IIS service in order to continue Setup, the installer starts restarting the IIS service, and the installer prompts the IIS service to restart successfully Click OK, and then click Finish to complete the installation.
5, after the installation of the program will automatically according to your choice to modify php.ini to help you start this engine. Here's a description of the Zend Optimizer configuration options to help you maximize your customized configuration.

[Zend]
ZEND_OPTIMIZER.OPTIMIZATION_LEVEL=15//My configuration file Zend_optimizer.optimization_level = 1023
zend_extension_ts= "C:inetpubzendlibzendextensionmanager.dll"/This is related to your installation path
Zend_extension_manager.optimizer_ts= "c:inetpubzendliboptimizer-2.5.5"

Now let's introduce the implications of the relevant content in these configuration files:
Zend_optimizer.optimization_level the degree of optimization, here defines how many optimization processes to start.
Zend_optimizer.encoder_loader = = Whether to allow processing of PHP files encrypted by Zend Encoder is allowed by default.
Zend_extension_ts = = The directory where the optimizer is located.
Zend_extension_manager.optimizer_ts "= = the optimizer manages in the directory.

Optimization Process Zend_optimizer.optimization_level Detailed explanation

Here is the most important part, look carefully! Zend opt has a total of 10 optimization processes, the more theoretically the more performance the better. Of course, there is always a gap between theory and practice. The more the optimization process is opened, the more the performance consumption is, and the 10 optimization processes of Zend opt are not the same, which means that the effect is not average. The highest value (high mode) defined by the Zend Company is 15, where 15 refers to the process of opening the 1-4 optimization. Of course, many friends are not satisfied with this, after all, the highest is only open 4 optimization process even half of the total did not. The corresponding numeric codes (values) for each optimization process are as follows:

Do not use 0 <= if this is not installed, but also save some memory say!
Optimization Process 1 (PASS1) 1
Optimization Process 2 (PASS2) 2
Optimization Process 3 (PASS3) 4
Optimization Process 4 (PASS4) 8
Optimization Process 5 (PASS5) 16
Optimization Process 6 (PASS6) 32
Optimization Process 7 (PASS7) 64
Optimization Process 8 (PASS8) 128
Optimization Process 9 (PASS9) 256
Optimization process (PASS10) 512

The way in which the optimization process is opened is controlled by the addition of these numeric codes (values) and the parameter values that are used as the parameters. As in my configuration file, Zend_optimizer.optimization_level = 1023, where 1023 is the sum of the optimization process 1 to the optimization process 10 all numeric codes (values), which indicates that all 10 optimization processes are turned on. The Zend Company-defined high mode value, as mentioned earlier, is 15,15 to open the optimization process at the same time 1-4.

Cryptographic code support Zend_optimizer.encoder_loader detailed explanation

For this parameter, I think most of the friends who have not seen the Zend Opt FAQ document do not know. This parameter is used to tell Zend opt whether to support code that has been Zend Encoder encrypted. By default, Zend Opt will support encrypted code. If you do not use the encrypted code I recommend that you turn off this option. This function is involved in the process of unpack and reverse code, which will result in heavier load of the system. My friend has developed a corresponding counter-compiling tool, which is expected to be launched after the Spring Festival.

The value of this parameter is only two 0 off, and 1 is open. The default is 1, and the recommended setting is 0.

Module positioning zend_extension_ts, zend_extension_manager.optimizer_ts do not need to explain, these parameters are Zend Optimizer related modules on the hard disk installation path.

6, how to learn Zend Optimizer is running?
The answer is simple, as long as the PHP function phpinfo () to detect, as follows:
?
Phpinfo ();
?>

The portion of the results shown about Zend Optimizer as follows: (PHP version is different, the information may be different, mine is PHP4.3.11)

CODE: [Copy to Clipboard]
This program makes the Zend scripting Language Engine:
Zend Engine v1.3.0, Copyright (c) 1998-2004 Zend Technologies with Zend Extension Manager v1.0.6, Copyright (c) 2003-2004, By Zend Technologies and Zend Optimizer v2.5.7, Copyright (c) 1998-2004, by Zend Technologies

Indicates that Zend Optimizer for PHP optimization succeeded.

PHP optimized Configuration bis
Accelerate with Eaccelerator (predecessor Truck MMCache)
What is Eaccelerator?
----------------------
Eaccelerator is a free open source PHP Accelerator, optimizer, encoder and
Dynamic content cache for PHP. It increases performance of PHP scripts by
Caching them in compiled state, so this overhead of compiling is almost
Completely eliminated. Also it uses some optimizations to speed up execution
of PHP scripts. Eaccelerator typically reduces server load and increases the
Speed of your PHP code by 1-10.

Eaccelerator is a fork of Turckmmcache
Eaccelerator (formerly Truck MMCache)

I believe many friends know MMCache, but a long time ago the developer was Zend Amnesty, so MMCache has been no official new version ... To support the latest PHP4.4.0 or PHP5.04 need to compile their own source code, for ordinary users is tantamount to the Arabian Nights ... Don't be discouraged, we still have eaccelerator!.

1. First of all, we download the compiled module
http://www.arnot.info/eaccelerator/
Choose to download the eaccelerator that corresponds to your system and PHP version

2. Rename it to Eaccelerator.dll (win) or eaccelerator.so (Linux version) and move to your PHP extentions folder, whose path is defined in the php.ini of your system

3. According to the situation of your system, there are generally two kinds of installation methods, one according to Zend expansion, one is the PHP extension

It is recommended to follow the Zend extension, open the php.ini file, locate the Extension=php_zip.dll, and add the following
Win version

CODE: [Copy to Clipboard]
zend_extension_ts= "C:phpextensionseaccelerator.dll"//path depending on your system, the file name extension is also
Eaccelerator.shm_size= "16"//According to your system memory, default 16M, can be changed to 64M
Eaccelerator.cache_dir= "c:phptmp"//need to be created manually, win, such as c:phptmp, to ensure that his permissions are read and write, and do not move this directory later OH
eaccelerator.enable= "1"
Eaccelerator.optimizer= "1"
Eaccelerator.check_mtime= "1"
eaccelerator.debug= "0"
Eaccelerator.filter= ""
eaccelerator.shm_max= "0"
Eaccelerator.shm_ttl= "0"
eaccelerator.shm_prune_period= "0"
eaccelerator.shm_only= "0"
eaccelerator.compress= "1"
Eaccelerator.compress_level= "9"

Linux version

CODE: [Copy to Clipboard]
zend_extension= "/usr/lib/php4/eaccelerator.so"//path depending on your system, the file name extension is also
Eaccelerator.shm_size= "16"//According to your system memory, default 16M, can be changed to 64M
Eaccelerator.cache_dir= "/tmp/eaccelerator"//need to be created manually to ensure that his permissions are read and write, and do not move this directory later OH
eaccelerator.enable= "1"
Eaccelerator.optimizer= "1"
Eaccelerator.check_mtime= "1"
eaccelerator.debug= "0"
Eaccelerator.filter= ""
eaccelerator.shm_max= "0"
Eaccelerator.shm_ttl= "0"
eaccelerator.shm_prune_period= "0"
eaccelerator.shm_only= "0"
eaccelerator.compress= "1"
Eaccelerator.compress_level= "9"

If Safe mode is turned on, you must use "Zend_extension_ts" instead of "zend_extension".

Attach: Create cache directory under Linux

CODE: [Copy to Clipboard]
Mkdir/tmp/eaccelerator
chmod 0777/tmp/eaccelerator

PHP Extended Install mode, open php.ini file, find Extension=php_zip.dll, add below

CODE: [Copy to Clipboard]
extension= "eaccelerator.so"//path depending on your system, the file name extension is also the DLL (for example, C:phpextensionseaccelerator.dll) under Win system
Eaccelerator.shm_size= "16"
Eaccelerator.cache_dir= "/tmp/eaccelerator"//need to be created manually, win, such as c:phptmp, to ensure that his permissions are read and write, and do not move this directory later OH
eaccelerator.enable= "1"
Eaccelerator.optimizer= "1"
Eaccelerator.check_mtime= "1"
eaccelerator.debug= "0"
Eaccelerator.filter= ""
eaccelerator.shm_max= "0"
Eaccelerator.shm_ttl= "0"
eaccelerator.shm_prune_period= "0"
eaccelerator.shm_only= "0"
eaccelerator.compress= "1"
Eaccelerator.compress_level= "9"

Eaccelerator can be compatible with Zend, but before the code in php.ini must define the relevant code for Zend, it is strongly recommended that you do not use Zend and Zend at the same time unless you have to use Eaccelerator code. But two together also not conflict, still can accelerate, why not, hehe

Save your php.ini after completing the above work, and then restart Apache or IIS to see if Phpinfo is different? And then see if the Phpwind footer is running much faster?
After proper installation, you can see the following information in the Zend section on the Phpinfo: (PHP version is different, the information may be different, mine is PHP4.3.11, general appearance with Eaccelerator is successful)

CODE: [Copy to Clipboard]
This program makes the Zend scripting Language Engine:
Zend Engine v1.3.0, Copyright (c) 1998-2004 Zend Technologies with Eaccelerator v0.9.3, Copyright (c) 2004-2005 Eaccelerat Or, by Eaccelerator with Zend Extension Manager v1.0.6, Copyright (c) 2003-2004, by Zend Technologies with Zend Optimizer v2.5.7, Copyright (c) 1998-2004, by Zend Technologies

It's simple. Before the operation as long as a good php.ini backup, failed a big deal will restore the back of the past. Start with Apache or IIS services, huh, the risk is not very big.


Then see the Forum after loading eaccelerator run, footer execution time from 0.02X seconds to 0.01X seconds ... Faster than a ^_^.
Special Note:

1, if your system has installed Zend Accelerator, then the above configuration in the php.ini must be placed in the front of the Zend, or Apache can not start;
2, eaccelerator.shm_size= "64", here 64 is to speed up the system using cache memory, the default is 16M, according to your system memory adjustment;
3, eaccelerator.cache_dir= "C:phptmp", this is the accelerator working time to generate temporary files directory, please set up a folder in the corresponding directory to store cached files and read and write permissions, if the installation time the default directory space small recommended adjustment

Effect: Now the system has Zend and eaccelerator Accelerator, PHP execution speed is faster than the original, Zend optimization has been opened to the largest, the most obvious in the PHP system is BBS, and software download background program output HTML static page is very fast, And there's a pause when there's only Zend

Other programming optimization methods

1. If a method can be static, make a static declaration on it. The rate can be raised to 4 times times.

2.echo is faster than print.

3. Use Echo's multiple parameters (meaning to use a comma instead of a period) instead of a string connection.

4. Determine the maximum number of loops before executing the For loop, and do not compute the maximum value per loop.

5. Unregister those unused variables, especially large arrays, in order to free memory.

6. Avoid using __get,__set,__autoload as far as possible.

7.require_once () is costly.

8. Using the full path when containing the files will require less time to resolve the operating system path.

9. Using $_server[' request_time ' is better than time () if you want to know when the script starts executing (that is, the server receives the client request).

10. Functions do the same function instead of regular expressions.

The 11.str_replace function is faster than the Preg_replace function, but the STRTR function is four times times more efficient than the Str_replace function.

12. If a string substitution function can accept an array or character as an argument, and the parameter length is not too long, consider writing an extra paragraph of substitution code so that each pass parameter is a character, rather than writing a single line of code to accept the array as a query and replacement parameter.

13. Using the Select Branch statement (ie, switch case) is better than using multiple if,else if statements.

14. Using the @ Block error message is very inefficient.

15. Open the Apache mod_deflate module.

16. The database connection should be turned off when the use is complete.

$row [' ID '] is 7 times times more efficient than $row[id].

18. Error messages are costly.

19. Try not to use functions in the For loop, such as for ($x =0; $x < count ($array); $x) call the count () function once per loop.

20. Increments the local variable in the method, the speed is the fastest. Almost as fast as calling a local variable in a function.

21. Incrementing a global variable is twice times slower than incrementing a local variable.

22. Incrementing an object property (such as: $this->prop++) is 3 times times slower than incrementing a local variable.

23. Incrementing an undefined local variable is 9 to 10 times times slower than incrementing a predefined local variable.

24. Defining only a local variable and not calling it in a function also slows down the speed (which is equivalent to incrementing a local variable). PHP will probably check to see if there are any global variables.

25. The method call appears to be independent of the number of methods defined in the class, because I added 10 methods (both before and after the test method), but there was no change in performance.

26. Methods in a derived class run faster than the same method defined in the base class.

27. Call an empty function with one parameter, which takes as much time as 7 to 8 local variable increment operations. A similar method invocation takes approximately 15 times of local variable increments.

28. It is quicker to use single quotes instead of double quotes to contain strings. Because PHP searches for variables in a string enclosed in double quotes, single quotes do not. Of course, you can do this only if you don't need to include variables in the string.

29. When outputting multiple strings, it is faster to separate strings with commas instead of periods. Note: Only echo can do this, it is a "function" that can take multiple strings as arguments: ECHO is the language structure, not the real function, so the function is added double quotes.

30.Apache Parsing a PHP script is 2 to 10 times times slower than parsing a static HTML page. Use static HTML pages as much as possible and use less scripting.

31. Unless the script can be cached, it will be recompiled every time it is invoked. The introduction of a set of PHP caching mechanisms typically increases performance from 25% to 100% to exempt compilation overhead.

32. Try to do the cache, you can use memcached. Memcached is a high-performance memory object caching system that can be used to speed up dynamic Web applications and reduce database load. It is useful to cache the operation Code (OP code) so that the script does not have to recompile for each request.

33. When manipulating strings and having to test whether their lengths meet certain requirements, you will of course use the strlen () function. This function executes fairly quickly because it does not do any calculations and returns only the known string lengths stored in the Zval structure (c's built-in data structure for storing PHP variables). However, since strlen () is a function, it will be somewhat slower, because function calls go through a number of steps, such as lowercase letters, PHP is case-insensitive, and hash lookups follow the functions that are called. In some cases, you can use the isset () technique to speed up the execution of your code.

(Examples below)
if (strlen ($foo) < 5) {echo "Foo is too Short" $$}
(compare with the following tips)
if (!isset ($foo {5})) {echo "Foo is too Short" $$}

Calling Isset () happens to be faster than strlen (), because unlike the latter, Isset () as a language structure means that its execution does not require function lookup and lowercase. That is, you're actually not spending too much on the top-level code that verifies the length of the string.

34. When the execution variable $i is incremented or decremented, $i + + $i slower than the + +. This difference is specific to PHP and does not apply to other languages, so please do not modify your C or Java code and expect them to quickly become useless. + + $i faster because it requires only 3 instructions (opcodes), $i + + requires 4 instructions. A post increment actually produces a temporary variable, which is then incremented. And the predecessor increment increases directly on the original value. This is one of the most optimized processes, as Zend's PHP optimizer has done. Keeping this optimization in mind is a good idea, because not all command optimizer will do the same optimization, and there are a large number of Internet service providers (ISPs) and servers that do not have assembly instruction optimizer.

35. Object-oriented (OOP), object-oriented, often expensive, each method and object invocation consumes a lot of memory.

36. Arrays are also useful not to implement all data structures with classes.

37. Do not subdivide the method too much and think carefully about what code you really intend to reuse.

38. When you need it, you can always break the code into methods.

39. Try to use a large number of PHP built-in functions.

40. If there are a lot of time-consuming functions in your code, you might consider implementing them in C extensions.

41. Evaluation Test (profile) of your code. The inspector will tell you which parts of the code are consuming much of the time. The Xdebug debugger includes a test procedure that can be used to show code bottlenecks in general.

42.mod_zip can be used as an Apache module to instantly compress your data and reduce data transfer by up to 80%.

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.