What should I do if my boss always wants me to hire some java programmers...?
He (the old man) will always come to me after chatting too much with engineers from other camps outside and ask me, you always play with PHP (framework),
But in the future our website will become popular and the load will increase... PHP is in performance What should I do if I can’t handle it, or the security is not as good as jsp (actually java)?
I'm busy, so I'll just say it... (Now that I think about it, it seems a bit simple and crude...?)
My (original) answer is:
(Your old) question is False proposition...because Performance (load) is not a problem at all...
The reason is very simple, because our business is O2O business, if the performance goes up (the load comes), it means that offline customers must also come ...
Then (if) we are not stupid (if we are not particularly stupid) we should be able to cash in the cash flow...
My implication is: if the business starts, we should have money, if there is If you have enough money and can purchase enough hardware equipment, even using PHP, you can achieve high-load online operations...
Now that I think about it carefully, I should be more rigorous...
Now let's analyze the relevant performance of PHP And load problems... (especially when compared with solutions from the java camp)
First of all:
First look at the ranking changes of the PHP language (among all computer languages)
According to the "TIOBE Programming Language Ranking ” (Although the statistical method of the list is limited, it is still a good reference). In 2010, PHP ranked third among the world’s programming languages. It can be seen that the PHP language is the most powerful language in the Web field in the PC Internet era.
There was once a joke circulating among PHP programmers:
A woman: If you can make everyone in this forum quarrel, I will eat with you.
PHP Programmer: PHP is the best language in the world!
A certain forum has exploded, with all kinds of quarrels...
A certain woman: I’ve convinced you, let’s go!
PHP Programmer: Not today, I must convince them that PHP must be the best language.
Okay, let’s get down to business. The language itself is not good or bad, it just solves different problems in the scenarios where it is used. The Internet era is moving very fast. With the arrival of mobile Internet, in just over four years, the development of mobile technology has swept the world. At the same time, various languages are emerging, and PHP, which was once glorious, has dropped to sixth place from the original programming language list (December 2014 list). As a result, voices that bad-mouthed PHP emerged one after another.
However, Brother Niao (Hui Xinchen, one of the PHP language developers) shared a data at Qcon in 2014. Among the top 1 million websites in the world, 81.3% use web server scripts The language is PHP, which was 78.3% in the same period of 2013. In other words, PHP's use in Web services has not decreased, but in the mobile Internet wave, many other language technology applications have been added, and thus it has been diluted.
Recently, the performance comparison between PHP7 and HHVM has become a hot and controversial topic. Everyone is discussing and paying attention to which one is the future of PHP performance improvement.
The origin of HHVM (HipHop Virtual Machine)
HHVM is an open source PHP virtual machine that uses JIT compilation and other technologies to greatly improve the execution performance of PHP code. It is rumored that the execution performance of the current version of native PHP code can be improved by 5-10 times.
HHVM originated from Facebook. Many of Facebook's early codes were developed using PHP. However, with the rapid development of business, PHP execution efficiency has become an increasingly obvious problem. In order to optimize execution efficiency, Facebook began to use HipHop in 2008, which is a PHP execution engine. It was originally designed to convert Facebook's large amount of PHP code into C++ to improve performance and save resources. The performance of PHP code using HipHop is improved several times. Later, Facebook open sourced the HipHop platform and gradually developed it into the current HHVM.
1. Why is PHP slow?
PHP is slower than C/C++ level languages. In fact, the PHP language was not originally designed to solve computing-intensive application scenarios. We can roughly understand that PHP sacrifices execution efficiency in order to improve development efficiency.
We know that a big feature of PHP is the weak type feature. That is to say, I can define a variable at will and then assign it to various types of data at will. Take an int integer number as an example, in C language:
int num = 200; // Usually 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. The definition in PHP is 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 is stored, it is implemented using the above-mentioned "all-in-one" structure. In order to be compatible with PHP programmers' variable type "intrusion", PHP has been friendly to developers, but cruel to the execution engine. The memory consumption of a single variable may not be obvious yet. Once PHP arrays are used, the complexity increases exponentially (the implementation of arrays is HashTable). Then, when the Zend engine is executed, these PHP codes are compiled into opcode (PHP's intermediate bytecode, the format is somewhat similar to assembly), which is interpreted and executed line by line by the Zend engine.
Whether it is the connection operation of strings or the simple modification of arrays, etc., it is almost the rhythm of "a word from a PHP programmer, the Zend engine will break down". Therefore, for the same operation, PHP consumes more system resources such as CPU and memory than C. In addition, there are automatic memory recycling, variable type judgment, etc., which will increase the consumption of system resources.
For example, I used the quick sort function and the native sort function implemented in pure PHP to sort 10,000 integer numbers to make a time-consuming comparison. The results are as follows:
The native sort took 3.44 ms, while Our own implementation of the PHP function sort takes 68.79 ms. We found that there is a huge gap in execution efficiency between the two. My way of testing is to calculate the time interval before and after the function is executed, not the time from start to end of the entire PHP script. The PHP script startup and shutdown process itself involves a series of initialization and cleanup work, which also takes up a lot of time.
Normally, the ranking of PHP execution efficiency is:
In general, we do not recommend using PHP to implement complex logical calculation functions , especially in scenarios where the Web system traffic is relatively large. Therefore, PHP programmers should have a relatively broad understanding of PHP's various native functions and various extensions. In specific function implementation scenarios, seek more native solutions (native interfaces or extensions) instead of writing one by themselves. Stack of complex PHP code to implement this type of functionality.
If you have enough PHP extension development capabilities, rewriting this type of business function as a PHP extension will also greatly improve the code execution efficiency. This is a very good method and is also widely used in PHP optimization. However, the disadvantages of self-written PHP business development are also obvious:
In fact, among front-line Internet companies, the more common solution is not to add PHP extensions, but to write a service server independently in C/C++, and then PHP communicates with the service server through sockets to complete business processing. PHP itself is coupled with the business.
However, most of the performance bottlenecks of web services are in the time consuming of network transmission and other service servers (such as MySQL, etc.). The time consuming of PHP execution accounts for a very small proportion of the overall time consuming, so from a business perspective , the impact may not be obvious.
2. How HHVM improves PHP execution performance
The way HHVM improves PHP performance is to replace the Zend engine to generate and execute PHP intermediate bytecode (HHVM generates its own format of intermediate bytecode), and executes It is converted into machine code for execution through JIT (Just In Time, just-in-time compilation is a software optimization technology, which means that the bytecode is compiled into machine code only at runtime). The default approach of the Zend engine is to first compile it into opcode and then execute it one by one. Usually each instruction corresponds to a C language level function. If we generate a large number of repeated opcodes (codes and functions written in pure PHP), Zend will execute these C codes one by one multiple times. What JIT does is to go one step further and compile a large number of repeatedly executed bytecodes into machine code at runtime to improve execution efficiency. Usually, the condition that triggers JIT is that the code or function is called multiple times.
Ordinary PHP code, because the type of the variable cannot be fixed, additional logic code to determine the type needs to be added. Such PHP code is not conducive to CPU execution and optimization. Therefore, HHVM usually needs to use PHP code with Hack writing method (additional technical code added to be compatible with certain features) to "cooperate", in order to fix the variable type and facilitate virtual machine compilation and execution. PHP pursues to accommodate all types in one form, while Hack can mark everything accommodated with a certain type.
Examples of Hack writing of PHP code:
In the above example, the PHP code is mainly added with variable types. The overall direction of Hack writing is to change the previous "dynamic" writing method into a "static" writing method to cooperate with HHVM.
HHVM has attracted a lot of attention because of its high performance, and some first-tier Internet companies have also begun to follow suit. Judging from the pure language execution performance test results, HHVM is much ahead of the PHP7 version under development.
However, from the perspective of specific business scenarios, the gap between HHVM and PHP7 is not that big. In the results of the test scenario using the WordPress open source blog homepage, their current gap is not obvious.
However, PHP7 is still under development. Judging from the available technical solutions, the current HHVM is slightly better. However, there are some problems in the deployment and application of HHVM:
HHVM is a relatively new open source project after all, and it will still take some time to mature.
Performance Innovation of PHP7
The performance issues that PHP has long been criticized for will be greatly improved in this version. There is no PHP6 in the middle of the version. It is said that this version has a project, and later most of the functions were implemented in the 5.x version. In order to avoid confusion, the next major version will be PHP7 directly. (A few years ago, I also read books about PHP6.)
1. Introduction to PHP7
Although the official version of PHP7 may not be released until October 2015, a test should be available in June next year version, and then there is a 3-4 month quality assurance.
The project plan of the PHP community is as follows:
Because the project is still under development, the feature descriptions that can be seen from the table are relatively vague. There are definitely more other features, they just haven't been announced yet. The following are from the PHP community. Because PHP7 is a project under development, the following may not be accurate, but it does not prevent us from taking a look.
Among the above features, the most anticipated is the performance optimization of PHPng. The PHP community has released some performance speed test data. From the data point of view, the execution performance of PHPng has been nearly doubled compared to the beginning of the project. This result is already very good. Moreover, the most important thing is that there are still many optimization plans for PHP7 that have not yet been completed. When everything is completed, I believe we can see a PHP7 with higher performance.
This speed test data comes from the PHP community (wiki.php.net/phpng), and intercepts part of the data:
For the current PHP5.6 version, the performance improvement of PHPNG in October has been very obvious :
Simple translation:
2. PHP’s weak type controversy
PHP has many controversial features, but with the release of language versions and Criticisms in terms of refinement, functionality, and features started to fade away. However, PHP's "weak type" feature has obviously been more controversial. From the fact that HHVM directly "removed" the "weak type" feature through Hack, it can be seen that HHVM does not like the "weak type" feature. However, in the eyes of many of us PHP programmers, this is one of the important advantages of PHP. Variables in PHP are designed to be casual and elegant, embracing everything. Doesn’t it make the language simpler?
In fact, some people think it is a serious problem. The criticism of "weak typing" is roughly as follows:
They believe that these are not in line with "what they see" That’s what you get” simplicity, while a language with strict grammar is more efficient and easier to “understand”.
Languages such as Javascript have also been similarly criticized because they perform the same on this issue. However, if a language is eventually used on a large scale, it must have its reasons. PHP has become the scripting language of choice for Web service development, and Javascript has directly dominated the Web front-end field. It cannot be an accident that it has reached this point. Developers voted for them with their feet. Programming language is a bridge between humans and machines, and the ultimate pursuit is to achieve the grand goal of “everyone can program”.
Throughout the history of language development, we started from the machine code of 0s and 1s, to assembly language, then to C language, and then to the dynamic scripting language PHP. The execution efficiency decreases exponentially, but the learning threshold also decreases exponentially. The PHP language not only shields the complexity of C's memory management and pointers, but also further shields the complexity of variable types. It improves the efficiency of project development and lowers the threshold for learning, but at the same time sacrifices a certain amount of execution performance. Then, HHVM's Hack gives us a "return to primitive" feeling, reintroducing the complexity of variables. Of course, different languages solve problems in different scenarios and cannot be generalized.
Summary
HHVM’s performance improvements to PHP are impressive, and the hard-working PHP7 is highly anticipated. Both are excellent open source projects and both are constantly moving forward and developing. For now, because it is still a long time before the official version of PHP7 is released, the current choice of performance optimization solution is of course HHVM. However, personally, I am more optimistic about PHP7 because it is more backward compatible with PHP code. If there is not much difference in performance between the two, I will choose the simpler one.
References:
The above has introduced PHP Food and Clothing Parent 3-php using HHVM to achieve high performance, including aspects of the content. I hope it will be helpful to friends who are interested in PHP tutorials.