Home >Backend Development >PHP Tutorial >A brief analysis of using Turck-mmcache compilation to accelerate and optimize PHP code_PHP tutorial

A brief analysis of using Turck-mmcache compilation to accelerate and optimize PHP code_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:04:571046browse

php_screw is indeed very good, but it can only play a role in encryption and has no acceleration or optimization effects.
Let’s take a look below:Turck MMCache
Its function is to accelerate and optimize php code by compiling php code. If the php code we publish is After compilation, it can also play some role in protecting the code.
Let’s compile compute.php and see the difference between the before and after code. The following is the source code of compute.php
"; echo "It used:"; echo $t1-$t; echo "seconds"; ?>
The following is the compiled compute.php file

Copy the code The code is as follows:

< ?php if (!is_callable("mmcache_load") && !@dl((PHP_OS=="WINNT"||PHP_OS=="WIN32")?" TurckLoader.dll":"TurckLoader.so")) { die(" This PHP script has been encoded with Turck MMcache, to run it you must install Turck MMCache or Turck Loader");} return mmcache_load('eJzz9XV2dPZwZWBiYGBgZABRzAwgoM0AAakggoODIxgozQxUwJDIqMbIyMDIiCy ZBqVtGMCmMDKzAOmSzNzUdHTdJYxqTIwMLIwYxmYiGRvACBcUBqplZGzojYbbpSvMxCARhtCmBNSjxQ OTRdIahn CxJNhOLQGs7mXC5V5QkJQYAt3FwMDCBNOqwcgMC iCbAjskEQ4gy7NEobQ4NcUK7hImuJeZgJazMGkwMSBpYQeyilOT8/NSipFEQeoV7BghwQAEAM45H7Y=');?>

No It is difficult to find. By publishing the compiled program, it should be able to effectively prevent the program from being tampered with.
Website
http://turck-mmcache.sourceforge.net/
How to get turck-mmcache
1 can be obtained through its website
2 Download from the following URL
http://prdownloads.sourceforge.net/turck-mmcache/turck-mmcache-2.4.6.tar.gz?download
Install
1. Put the downloaded turck-mmcache-2.4.6.tar.gz into /usr/local and decompress it
tar zxvf turck-mmcache-2.4.6.tar.gz
2. Enter the turck-mmcache-2.4.6 directory and configure
cd turck-mmcache-2.4.6 export PHP_PREFIX="/usr" $PHP_PREFIX/bin/phpize ./configure --enable -mmcache=shared --with-php-config=$PHP_PREFIX/bin/php-config
3. Compile and install
make make install
4. Modify/ php.ini file in etc/apache/ directory, in order to use turck-mmcache.
Add the following content to the php.ini file:
Copy the code The code is as follows:

extension="mmcache.so" mmcache.shm_size="16" mmcache.cache_dir="/tmp/mmcache" mmcache.enable="1" mmcache.optimizer="1" mmcache.check_mtime="1" mmcache .debug="0" mmcache.filter="" mmcache.shm_max="0" mmcache.shm_ttl="0" mmcache.shm_prune_period="0" mmcache.shm_only="0" mmcache.compress="1"

5. Restart Apache
/etc/rc.d/rc.httpd restart
After the above 5 steps, turck-mmcache is ready to use. Next we will accelerate and optimize A simple test of the effect.
Test
1. Write a compute.php file to perform a large number of loop calculations, take the system time before and after the calculation, and finally subtract the two times to get the time spent. (Just a rough estimate)
The content of the compute.php file is as follows
"; echo "It used:"; echo $t1-$t; echo "seconds"; ?>
2. Calculation Let’s take a look at the time it takes when it is not compiled, that is, calling the compute.php file I just wrote in the browser. You need to wait patiently for a while, about 30 seconds (under p3 500 CPU)
3. In order to compile php files conveniently, let's write a shell called encoder to compile php files.
<1> First copy the encoder.php file in the /usr/local/turck-mmcache-2.4.6 directory to the /usr/bin directory
cp /usr/local/turck- mmcache-2.4.6/encoder.php /usr/bin/
<2> Use vi encoder to create the encoder file, the content is as follows:
#!/bin/sh clear src=$1; echo $src; src2 =$src".en"; echo $src2; cp /usr/bin/encoder.php ./encoder.php php encoder.php $src -o $src2; rm encoder.php mv $src $src.bak; mv $src.en $src;
<3> Give executable permissions to the shell you just wrote and move it to the /usr/bin directory
chmod +x encoder mv encoder /usr/bin/
<4> Compile the compute.php file through encoder. After compilation, compute.php is the compiled file. The source file is named compute.php.bak and saved
encode compute.php
<5> in the browser Call the compiled compute.php file and see if the speed improves?
In my case, it originally took 27 seconds, but after compilation it took 22 seconds

The difference between php_screw and turck-mmcache
php_screw is just a simple pair PHP code encryption has no acceleration and optimization effects.
Turck-mmcache compiles PHP code so that the speed of PHP code interpretation and execution can be close to the speed of binary code. It can speed up and optimize PHP code. However, the code compiled by turck-mmcache only turns the source code into binary code through certain rules, and there is no real encryption.
So if you want to ensure the absolute security of the code, use php_screw. If you consider factors such as speed, use turck-mmcache.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/327733.htmlTechArticlephp_screw is indeed very good, but it can only play the role of encryption, and does not have the effect of acceleration and optimization. Let's take a look below: Turck MMCache Its function is to compile php code...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn