He (old person) outside and other camp engineers talk more and will always come to ask me, you always play PHP (framework),
But after our website fire, load up ... PHP does not carry the performance, or security than JSP (in fact, Java) to do?
I'm busy, so just a word to shoot it ... (now think of a bit of a simple and rude suspicion ...?) ）
My (original) answer is:
The question raised by you (old) is a pseudo-proposition ... Because performance (load) is not a problem at all ...
The reason is very simple, because our business is to do business, if the performance up (load come), that the customer also came to a certain line ...
Then (if) we are not fools (not particularly stupid) should be able to cash flow ...
The implication of my words is: If the business is done, we should have money, if you have money, you can buy enough hardware, even with PHP, can also achieve the burden of high-load online operations ...
Now think carefully, should be more rigorous ...
Now let's analyze PHP's performance and load issues ... (especially in comparison with the Java Camp solution)
First of all:
First look at the change in the ranking of the PHP language (in all computer languages)
According to the "Tiobe programming language Rankings" (although the list is limited, but still a good reference), 2010 PHP was the highest in the World programming language ranked third. It can be seen that the PHP language in the PC Internet era of the web domain is masterful, Dynasky one column.
In PHP programmers, there was a joke:
A woman: You can make this forum people fight up, I will eat with you.
PHP Programmer: PHP is the best language in the world!
A forum fryer, all kinds of quarrel ...
A woman: Take you, let's go!
PHP Programmer: Not today, I have to convince them that PHP must be the best language.
Well, we got to the point where the language itself is not good or bad, but that it solves different problems in its own scenario. The era of the Internet wheel is fast, with the advent of mobile internet, in just over four years, mobile technology development sweeping the world. At the same time, a variety of languages, and the former brilliant PHP from the original programming language list, down to the sixth place (December 2014 list). As a result, the sound of a bad PHP.
However, Xinchen, one of the PHP language developers, has one data in the 2014 Qcon share, the world's top 1 million web site, 81.3% using the Web server-side scripting language is php,2013 year-over-period is 78.3%. In other words, PHP does not reduce the Web services, but in the wave of mobile internet, adding a lot of other language technology applications, and then diluted.
Recent performance comparisons with PHP7 and HHVM have become a hot topic of contention, and everyone is discussing and focusing on which is the future of PHP's performance improvement.
The origins of HHVM (HipHop Virtual machine)
HHVM is an open-source PHP virtual machine that uses JIT compilation and other techniques to dramatically improve the performance of PHP code execution. It is rumored that the current version of the native PHP code can be improved by 5-10 times the execution performance.
HHVM originated from Facebook, and many of Facebook's early-morning code was developed using PHP, but with the rapid development of the business, the efficiency of PHP execution became more and more obvious. To optimize execution efficiency, Facebook started using hiphop in 2008, a PHP execution engine originally designed to turn Fackbook's massive PHP code into C + + to improve performance and conserve resources. PHP code that uses hiphop has several times the performance boost. Later, Facebook will open up the hiphop platform and evolve into the current HHVM.
1. Why is PHP slow?
The slowness of PHP is relative to the C + + level language, in fact, the original design of the PHP language is not used to solve computational-intensive scenarios. We can roughly understand that PHP has sacrificed execution efficiency in order to improve development efficiency.
We know that PHP is a big feature of the weak type feature, that is, I can arbitrarily define a variable, and then give it arbitrarily assigned to various types of data. Take an int integer number as an example, in the C language:
int num = 200;//is typically 4 bytes
However, if PHP defines the same variable, the actual corresponding storage structure is:
This structure will occupy much more memory than the C variable, which is defined in PHP as follows:
$a = 200;//This variable will actually occupy many times the storage space compared to the C variable.
In fact, for PHP, no matter what type of data stored, are used in the above "kill" the structure of the implementation. In order to be compatible with the PHP Programmer's Variable type "random", PHP is a developer friendly, but the execution of the engine is very cruel. The memory consumption of a single variable may not be obvious, and once the array of PHP is used, the complexity index goes up (the implementation of the array is hashtable). Then, when the Zend engine executes, the PHP code is compiled into opcode (PHP's intermediate bytecode, which is a bit like a compilation), which is performed by the Zend engine line by row.
Whether it is a string connection operation, or an array of simple modifications, and so on, almost all are "PHP programmer a word, Zend engine run broken leg" rhythm. Therefore, the same operation, compared to C, PHP consumes more CPU and memory system resources. In addition, there are automatic memory recovery, variable type judgment and so on, will increase the consumption of system resources.
For example, I use a pure PHP implementation of the fast sorting function and the native sort function, sorting 10,000 integer numbers, to do a time-consuming comparison, the results are as follows:
Native sort time 3.44 ms, and our own PHP function sort is 68.79 Ms. We find that the efficiency gap between the two is huge. My test method is to calculate the time interval before and after the function is executed, not the entire PHP script from the start to the end of the time. PHP script startup and shutdown process, itself has a series of initialization and cleanup work, will also occupy a lot of time.
Typically, the ranking of PHP execution efficiency is:
In general, we do not recommend the use of PHP to implement logical complex computing types of functions, especially the web system traffic is relatively large scenario. Therefore, PHP programmers should have a broad understanding of PHP's various native functions and various types of development, in the specific functional implementation scenario, to seek a more native solution (native interface or extension), rather than write a bunch of complex PHP code to implement this type of functionality.
If you have enough PHP to expand your development capabilities, rewriting this type of business function as a PHP extension will also significantly increase the efficiency of your code execution. This is a very good way to also be widely used in PHP optimization. However, the shortcomings of the PHP business development that you write are also obvious:
In fact, in the Internet front-line enterprise, the more common solution is not to increase the PHP extension, and write a service server in C + +, and then PHP through the socket and service server communication to complete business processing, not the PHP itself and the business coupling.
However, most of the performance bottlenecks in Web services are time-consuming for network transport and other service servers (such as MySQL, etc.), and the time-consuming implementation of PHP is very small in overall time-consuming proportions, so the impact may not be obvious from a business perspective.
2. HHVM ways to improve PHP execution performance
HHVM the way to improve the performance of PHP, by replacing the Zend engine to generate and execute PHP Intermediate bytecode (HHVM generate its own format of intermediate bytecode), execution through the JIT (Just in time, instant compilation is a software optimization technology, Refers to the machine code to be compiled at run time. The default practice of the Zend Engine is to compile the opcode first and then execute it one after the other, usually each instruction corresponds to a C-level function. If we produce a lot of duplicate opcode (pure PHP code and functions), the corresponding is Zend to execute the C code several times. The JIT does is to further, the large number of repeated execution of bytecode at run time to compile into machine code, to improve execution efficiency. Typically, the condition that triggers the JIT is that the code or function is repeatedly called repeatedly.
Normal PHP code, because the type of the variable cannot be fixed, need to add additional logic code to determine the type, so that PHP code is not conducive to CPU execution and optimization. Therefore, HHVM usually need to use the hack code (in order to be compatible with a particular feature and additional skills of the nature of the code) to "match," is to let the variable type fixed, convenient for virtual machine compilation execution. PHP seeks to accommodate all types in one form, while hack can mark the type of everything that is to be accommodated.
Examples of PHP code hack notation:
In the above example, the PHP code is mainly added to the variable type. Hack the overall direction of writing, is to change the previous "dynamic" to the "static" writing, to cooperate with the HHVM.
HHVM has attracted a lot of attention because of its high performance, and some first-line internet companies have started to follow suit. From pure language performance test results, HHVM lead the development of the PHP7 version of a lot.
However, from the specific business scenario, HHVM and PHP7 gap is not so big, to WordPress open Source Blog Home for the test scenario results, their current gap is not obvious.
However, PHP7 is currently in development, in terms of the available technical solutions, the current HHVM notch above. However, there are some problems with the deployment and application of HHVM:
After all, HHVM is a relatively new open source project, and it still takes some time to grow to maturity.
PHP7 's performance innovation
PHP has long been criticized for performance issues that will be greatly improved in this version. There is no PHP6 in the middle of the version, it is said, because this version has been a project, and then most of the functions are implemented in the 5.x version, in order to avoid confusion, the next major version is directly PHP7. (A few years ago, I saw books about PHP6.) ）
1. Introduction of PHP7
Although the official version of PHP7 may not be released until October in 2015, a beta version should be visible next June, followed by a quality assurance of 3-4 months.
The project plan for the PHP community is as follows:
Because the project is still in development, the descriptions of the features that can be seen from the table are blurred. There must be more of the other features, just not yet announced. These are seen from the PHP community, because PHP7 is a project in development, and these are not necessarily accurate, but it does not prevent us from looking together.
Among the above features, the most anticipated is Phpng performance optimization, the PHP community has released some performance of the speed data. From the data, the performance of Phpng is nearly 1 time times higher than the start of the project. This is a very good result, and the most important thing is that PHP7 's optimization plan is still a lot of unfinished. When all is done, I believe we can see a higher performance PHP7.
The velocity data is from the PHP community (wiki.php.net/phpng), which intercepts a subset of the data:
For its current PHP5.6 version, Phpng's October performance improvement has become apparent:
Under Simple translation:
2. PHP's weak type controversy
PHP is controversial in many ways, but with the release and refinement of language versions, criticism of functionality and features has begun to grow less. However, PHP's "weak type" characteristics, but obviously more controversial, from HHVM through the hack way directly "remove" the "weak type" feature can be seen, hhvm do not like the "weak type" feature. However, in the eyes of many of our PHP programmers, this is one of the important advantages of PHP. PHP variables are designed with the nature and elegance, the Sea of the rivers, all can be tolerated, not to make the language more simple?
In fact, some people think it is a serious problem, and the criticisms of "weak type" are as follows:
These, they argue, are not in line with the simplicity of what is WYSIWYG, and the language is more efficient and easier to understand.
Throughout the history of language development, from 0 and 1 of machine code, to assembly language, then to C, then to the Dynamic scripting language PHP. The efficiency of execution decreased exponentially, but the learning threshold also decreased exponentially. The PHP language not only masks the complexity of C's memory management and pointers, but also shields the complexity of variable types further. Improve the efficiency of project development, reduce the threshold of learning, but at the same time sacrificing a certain degree of execution performance. Then, HHVM's hack gives us a feeling of "returning to the original" and re-introducing the complexity of the variable. Of course, different languages solve the problems in different situations, and can not generalize.
HHVM to PHP performance improvement, let a person in front of the light, and sharpening PHP7 is very look forward to. Both are extremely good open source projects, are constantly moving forward and development. For now, since the release from PHP7 is still a relatively long time, the choice of the current performance optimization scheme is, of course, HHVM. However, as far as I am concerned, I am more optimistic about PHP7, because it is more capable of backwards compatibility of PHP code. If the performance difference is not small, I will choose the simple one.
The above describes the PHP food and clothing parents 3-php use HHVM to achieve high performance, including aspects of the content, I hope that the PHP tutorial interested in a friend helpful.