Descridemo-de_oncerequirerequire_once performance comparison in php _ PHP Tutorial

Source: Internet
Author: User
In php, the performance of includemo-de_oncerequirerequire_once is detailed. The article detailed the include, include_once, require, and require_once performance diagrams in php. if you need them, please take a look at them in detail, however, in general, include_once and the article describe in detail the include, include_once, require, and require_once performance diagrams in php. if you need them, please take a look at them in detail, but in general, the performance of include_once and require_once is much better, especially in multiple calls.

PHP performance optimization stage 8 function, include (), include_once (), require (), and require_once () performance comparison, mainly by obtaining the execution time of the four functions to compare performance differences, this experiment uses the Benchmark_Iterate tool.

Test method
Load an existing file exist. php and call it 50 times through the Benchmark_Iterate class to obtain the execution time of each function and generate a graph.

Test Code

The code is as follows:

Require_once "Benchmark/Iterate. php ";
$ Tenant = new Benchmark_Iterate;
Function load_include (){
Include 'Exist. php ';
}
Function load_require (){
Require 'Exist. php ';
}
Function load_include_once (){
Include_once 'Exist. php ';
}
Function load_require_once (){
Require_once 'Exist. php ';
}
$ Response-> run (50, "load_include ");
// $ Response-> run (50, "load_require ");
// $ Response-> run (50, "load_include_once ");
// $ Response-> run (50, "load_require_once ");
$ Result = $ response-> get ();

Test results
1. execution time of file loading using the include () function


Illustration: The average execution time of file loading using the include () function is about 0.0013 seconds.

2. execution time of file loading using the include_once () function

Illustration: The average execution time of file loading using the include_once () function is 0.0011-0.0012 seconds.

3. use the require function to load the file execution time

Illustration: The average execution time of file loading using the require () function is 0.0012-0.0013 seconds.

4. use the require_once () function to load the file execution time

Solution: The average execution time of file loading using the require_once () function is 0.0011-0.0012 seconds.

According to the test results above, the execution time of the four functions to load files is almost the same. The difference is that if a file is loaded multiple times, the include and require functions load files multiple times, the include_once and require_once functions are loaded only once. Processing load failures at the same time is different. include () and include_once () generate a warning while require () and require_once () lead to a fatal error.

Description
Include () and require () functions
These two structures are identical except for how to handle failures. Include () generates a warning and require () causes a fatal error. In other words, if you want to stop processing the page when a file is lost, use require (). This is not the case with include (). The script will continue to run. Make sure that the appropriate include_path is set. Note that before PHP 4.3.5, the program will not be stopped due to a syntax error in the inclusion file, but will be executed after this version.

Find the file inclusion sequence first in the relative include_path of the current working directory, and then in the relative include_path of the directory where the current running script is located. For example, include_path Is ., the current working directory is/www/, and the script should include an include/. php and include "B. php ", then look for B. the php sequence is/www/, and then/www/include /. If the file name starts with./or./, you can only search for it in the include_path Directory of the current working directory.

When a file is included, the code contained in the file inherits the variable range of the row where the include is located. From this point on, any variables available to the calling file in this row are also available in the called file. However, all functions and classes defined in the inclusion file have a global scope.

Include_once () and require_once () functions
The include_once () and require_once () statements include and run the specified file during script execution. This behavior is similar to the include () and require () statements. The only difference is that if the code in the file has been included, it will not be included again. As the statement name implies, it will only be included once. Include_once () and require_once () should be used when the same file may be contained more than once during script execution, to ensure that it is only included once to avoid function redefinition, variable re-assignment and other issues.


...

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.