Performance Comparison between require and file_get_contents when PHP loads large files

Source: Internet
Author: User
When PHP loads large files, the performance of require and file_get_contents is compared. during the development process, it is found that when require is used to load a large (several hundred K or even several megabytes) configuration file, response timeout may occur. If you serialize the content of this configuration file and use file_get_contents to obtain the file and then deserialize the file to load it, it will be much faster.

After nearly two weeks of research, I know the reasons.

First, let's start with the PHP process. PHP actually has two processes: one is the start process and the other is the request response process. As a module of Apache, PHP registers two functions with Apache: sapi_startup, and php_handler, which is called when Apache receives the request.

Start process:

Apache startup

-> Sapi_startup

-> Php_module_startup (PHP startup master switch)

-> Zend_startup (starts the Zend Engine, including initializing global variables, and initializing compile and execute functions


Request process:

Apache receives the request

-> Sapi_startup

-> Zend_activate (including initializing the compiler, initializing the actuator, and starting the scanner)

-> Zend_compiler (syntax analysis, semantic analysis, and opcode generation)

-> Zend_execute (execute each opcode)

-> Zend_deactive (clear the data used in this request)

If you encounter a function such as require or include, it will return to the zend_compiler stage from the zend_execute stage to explain PHP and execute PHP.

In addition to the zend_compiler and zend_execute phases, require and file_get_contents have the same overhead.

Besides, apc extensions are installed on our servers, which means the zend_compiler stage can be considered the same.

Their performance is nine times worse than zend_execute.

First, let's use the vld extension to check the number of opcodes generated by the two files, because this is the execute input.

The results show that the number of opcodes generated by require is more than 20 thousand, most of which are ADD_ARRAY_ELEMENT, which means to construct data, while file_get_contents generates only 6 opcodes;

Then compare the execution efficiency:

The two functions can be executed in two parts: reading files and constructing arrays in configuration files;

First, read the file. The require read mechanism is to read the file into the memory cyclically in a buffer of 8192 bytes, while file_get_contents uses mmap to directly map the file to the virtual memory. In this case, require will have a lot more system calls than file_get_contents. File_get_contents does not require switching between the multi-user mode and kernel mode. In this step, file_get_contents wins;

Let's take a look at constructing an array. the mechanism constructed by require is to generate more than 20 thousand opcodes, and then execute these opcodes at one time. while file_get_contents uses the unserialize function, which parses the input text, then we construct an array step by step. Their idea of constructing arrays is the same, but the overhead of each level of data added by require is higher than that of unserialize; this is also the case of file_get_contents;

However, file_get_contents is a function call in PHP, while require is a built-in opcode. Therefore, the overhead of calling file_get_contents is slightly higher than that of require;

Therefore, when reading a small file, file_get_contents cannot take advantage of the memory ing when reading the file. The two are part of the same; when reading a large file, because require needs to call the read system in a 2K2K loop, the performance is reduced.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.