Does it make sense to minimize PHP?

I know you can minimize PHP, but I wonder if there is any point. PHP is an interpreted language, so it will work a little slower than a compiled language. My question is: will clients see noticeable speedups when loading pages, and if I were to minimize my PHP?

Also, is there a way to compile PHP or something similar?

+92
php minify
Nov 02 '10 at 16:38
source share
7 answers

PHP is compiled into bytecode, which is then interpreted on top of something like a virtual machine. Many other scripting languages ​​follow the same general process, including Perl and Ruby. This is not a traditional interpreted language such as BASIC.

There would be no effective increase in speed if you tried to "minimize" the source. You will get a significant increase with a byte of bytecode such as APC .

Facebook introduced a compiler called HipHop that converts the PHP source into C ++ code. Rasmus Lerdorf, one of the big PHP guys, made a presentation on HHVM - a new virtual machine based on their work creating HipHop. This is still fairly new, and it is unclear whether it will provide a significant increase in productivity for the general public.

To make sure this is stated explicitly, read this entire presentation . He points to numerous ways to compare both profile code and identify bottlenecks using tools like xdebug and xhprof , also from Facebook.

+154
Nov 02 '10 at 16:41
source share

Give up the idea of ​​minimizing PHP in favor of using an operation code cache, such as PHP Accelerator or APC .

Or something like memcached

+13
Nov 02 '10 at 16:42
source share

With some rewrites (shorter variable names) you can save several bytes of memory, but this is also rarely significant.

However, I am developing some of my applications in a way that allows me to combine scripts together. With php -w it can be greatly compacted by adding a slight speedup to run the script. On a server with opcode mode support, this, however, only saves a few checks of mtime files.

+3
Nov 02 '10 at 16:47
source share

This is less of an answer than advertising. I am working on a PHP extension that converts Zend code codes to work on a static typed virtual machine. This does not speed up arbitrary PHP code. This allows you to write code that runs faster than regular PHP allows. The key here is static typing. On a modern processor, a dynamic language is powered by incorrectly predicting branches left and right. The fact that PHP arrays are hash tables also comes at a great cost: a lot of incorrect industry predictions, inefficient cache utilization, poor memory prefetching and lack of SIMD optimization. Incorrect industry prediction and cache flaws, in particular, are the Achilles heel for modern processors. My small virtual machine circumvents this problem by using static types and a C array instead of a hash table. The result ends about ten times faster. This uses bytecode interpretation. An extension can optionally compile a function through gcc. In this case, you get two to five times more speed.

Here is a link for anyone interested:

https://github.com/chung-leong/qb/wiki

Again, the extension is not a generic PHP accelerator. You must write code for this.

+3
Mar 30 '13 at 23:01
source share

There are PHP compilers ... see this previous question for a list; but (if you are not the size of Facebook or do not aim the application to run on the client side), they are usually much more problems than they cost

Simple caching of the operation code will give you more benefits for your efforts. Or profile your code to identify bottlenecks, and then optimize it.

+1
Nov 02 '10 at
source share

You do not need to minimize PHP. For best performance, install the Opcode cache; but the ideal solution would be to upgrade your PHP to version 5.5 or higher, because newer versions have a default operation cache code called Zend Optimiser, which works better than others http://massivescale.blogspot.com/2013/06/php -55-zend-optimiser-opcache-vs-xcache.html .

+1
Jan 21 '15 at 13:01
source share

Yes, there is one (non-technical) point.

Your hoster can track your code on its server. If you reduce and reduce it, it will be more difficult for spies to steal your ideas.

One of the reasons for php reduction and decrease may be spyware protection. I think ugly code should take one step in automatic deployment.

0
Apr 28 '19 at 10:52 on
source share



All Articles