Regarding the execution efficiency of PHP, there are many special articles on the Internet, most of which are debates between the PHP and Java camps. Different application aspects and different execution environments will lead to a larger difference in efficiency. The "large" applications mentioned here do not refer to the specific implementation of large websites such as Google, eBay, and Yahoo. I just hope that your system can run faster and more smoothly, and can host more users online. I hope that it can give PHP A little help for beginners.
The "large" applications mentioned here do not refer to the specific implementation of large websites such as Google, eBay, and Yahoo. I have no intention of persuading readers to give up their concepts and beliefs. I just hope that everyone's systems can run faster and smoother. It can host more users online, and I hope it can give some help to PHP beginners.
Regarding the execution efficiency of PHP, there are many special articles on the Internet. Most of them start with the debate between the PHP and Java camps and end with uncertain expectations. It is rare to see a clear conclusion. Indeed, the execution efficiency of a program is difficult to derive from comparison. Different application aspects and different execution environments will lead to a larger difference in efficiency. And efficiency also needs to be weighed. Everyone knows that assembly language is very low-level and can write very efficient programs. However, I have rarely seen anyone use assembly for web development and have the ability to write programs in assembly. People who are efficient programmers seem to be worthy of everyone's respect, haha~ We don't need to discuss the difference between PHP and assembly, as long as we know the difference between our own PHP and other people's PHP.
First of all, let’s be clear about the premise of this article: there must be one or more servers that can be controlled by yourself, not a virtual host space. After all, there are already many classic works and mature frameworks for general-purpose systems that can run on virtual hosts. Efficiency mining has been done very well by predecessors. Many of their concepts have also been inherited and developed by many current PHP users. More and more The more so-called "frameworks" there are, they are like stars in the sky. I don't want to write about them anymore, because firstly, my own level is not very good, I can't write anything new, and even if I write it, it will make people laugh. Second, there are too many articles written about this, and there are too many opinions. Confusion is the biggest culprit that causes the death of many passionate future genius programmers.
There is a big difference in efficiency optimization between programs executed on a dedicated server and programs that can be run on a virtual host. Of course, you can install a set of discuz on one or even a bunch of independent servers without modification, but does it really get the maximum performance optimization? Are you really worthy of this bunch of servers?
An independent server means that the user has complete control over the machine, including installing and deleting software, configuring system parameters and even modifying source code. Based on such an open hardware platform, performance is not only reflected in speed, but also includes security, stability, etc. Unlike virtual hosts, users must configure web server parameters, install and configure PHP, databases, and install all kinds of messy things (as I like to say) by themselves, and of course they must be responsible for them.
First, let’s put forward a few terms: execution time, template, database encapsulation, Cache, Buffer, Hash, daemon, and crontab.
Everyone knows that execution time is the time it takes for a program to run from the beginning to the end. Because the Web is transient and stateless, execution time is an indicator of Web program execution efficiency. It is not suitable for measuring C/S programs or background daemon programs because many of them run continuously. A typical example of page execution time is the time display at the bottom of the Discuz forum page. Usually Discuz is from a few milliseconds to tens of milliseconds, depending on the platform used, the amount of data, and the current system pressure.
Everyone is familiar with templates. Although many people just use them, they don’t know why they use them. Templates are traditionally a way to divide logical layers. In the MVC upper structure, it separates the presentation layer from the lower layer. In actual use, it facilitates the division of labor between programmers and interface designers. However, in many cases now, due to the improper use of templates, instead of promoting the division of labor and cooperation between programmers and interface designers, it has become the main culprit of mutual hatred between programmers and artists (I think I said this in a previous post) , many people are complaining that they have to spend a lot of time organizing templates.
Database encapsulation seems to be more related to Java. It provides a unified calling interface for various database systems, usually some encapsulated classes. These classes sometimes also complete some tasks such as SQL checking, filtering, etc. DB packaging, PEAR DB, Adodb, etc. in PHPLIB are all famous and used by many people.
Cache and Buffer seem to be the same thing, Cache is called cache and Buffer is called buffering. In the hardware concept, the purpose of Cache is to connect two devices with different speeds, such as registers and memory, CPU and PCI-Bus, IDE bus and hard disk. The original meaning of Buffer is a spring-like buffer, something used to reduce or absorb the shock of impact. Buffer is a data pre-access method that is used to temporarily store data and transmit it at a speed different from the receiving speed. The Buffer update method can be automatically refreshed according to time intervals, while the Cache pays more attention to the "hit rate" and puts a small amount of data that is frequently used in the current time period into a high-speed device for easy reading and writing. In program development, although there are no high-speed or low-speed devices, data sources can have different reading and writing efficiencies. For a small amount of data, text file reading and writing are usually more efficient than database access, and the same text file reading and writing efficiency on tmpfs is better than direct disk IO efficiency. Buffer is more reflected in process communication and queues. In many cases, it is not because the receiver is unable to read faster, but because there is no need to read faster.
A daemon process is a program that is continuously executed in the background. It usually plays a role in monitoring, controlling processes, and providing external services. For example, Apache itself can be understood as a daemon process, although it is actually composed of many processes that are frequently updated (the main process is fixed).
Crontab is a UNIX/Linux scheduled program, which is a bit like Windows' "scheduled tasks". It sets a certain time interval or a certain point in time to execute a specific program. It is usually used to complete automatic updates, clear temporary data and other operations that are automatically performed once in a period of time.
Another special concept (especially for people who are used to general system development) is that after we have an independent server, there is no need to limit ourselves to the functions that PHP can provide. When we unknowingly become the masters of the system, we must work hard to realize this. There are many things we can use. PHP is not omnipotent (this is for sure), and its functional shortcomings can be completely compensated by Perl. As a general language, Perl can provide more functional options, and its modules are as dense as grit. The casual and perverted language provides endless energy. For PHP's performance deficiencies, C can be used to make up for it. The foundation of PHP is inherited from C. PHP itself is also developed by C. It is completely reasonable to use C to extend PHP.
Linux itself is supported by C and Perl (I say this not to exaggerate the status of Perl. You can see how many Perl scripts there are in a standard Linux and see if the system feels like a disabled person without Perl). PHP inherited most of its syntax from C, and learned most of the Web features, functions, and the "$" symbol that seems to be contradictory to open source from Perl (PHP was a Perl script in its early days).
Let’s analyze some of the code I’m using (Note: Applicable to Linux standalone servers. I seem to have given up on large-scale development for Windows and virtual hosts for a long time). It uses some methods that may be familiar, unfamiliar, or abnormal. My system is RedHat AS3, nothing special, the PHP version is 4.4.0, MySQL is 4.1. I never intentionally write code that must use the new features of PHP5 unless it is really necessary.
My Web root directory is under /www. Apache and PHP are installed under /usr/local/ by default. MySQL is a downloaded and compiled binary version, and I also leave it there. Because it's just for testing, I don't want it to look messy. As for actual projects, especially in the case of multiple servers, you need to deploy your system well.
In order to make the structure of the system clearer, I put all the files I need to use in the secondary directory.
Here are some snippets from the common header file /includes/kernel/common.inc.php:
﹤?php
if (!defined('IN_BSG')) {
exit;
}
?﹥
The above code ensures that it can only be called by legal programs and will not be included by other files. If the executing program does not define an 'IN_BSG' constant, it will terminate after including this common.inc.php.
﹤?php
list($usec, $sec) = explode(" ", microtime());
$page_time_start = $usec + $sec;
?﹥
Everyone may be familiar with these two lines, which are used to calculate the start execution time of the program. Before the program ends, this is calculated again to find out the time it took to execute the program. If you don't care about this, you can safely comment it out.