The article uses the graphic detailed introduction in PHP about include and include_once and require and require_once Performance Map, the need for friends can be detailed to see, but overall include_once and require_ Once performance is much better, especially when multiple calls are made.
PHP Performance Optimization Eighth phase function, include () and include_once () and require () and require_once () performance comparison, mainly by obtaining these four function execution time comparison performance difference, experiment using Benchmark_ The Iterate class tool.
Test method
Load an existing file exist.php, call through the Benchmark_iterate class 50 times, get the execution time of each function and generate a graph.
Test code
The code is as follows |
Copy Code |
Require_once "benchmark/iterate.php"; $bench = 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 '; } $bench->run ("Load_include"); $bench->run ("Load_require"); $bench->run ("load_include_once"); $bench->run ("load_require_once"); $result = $bench->get (); |
Test results
1, use the Include () function to load the execution time of the file
Plot: Average execution time for loading files using the Include () function is about 0.0013 seconds
2, load the file execution time using the include_once () function
Plot: Average execution time for loading files using the Include_once () function is 0.0011-0.0012 seconds
3, loading the file execution time using the Require function
Plot: Average execution time for loading files using the Require () function is 0.0012-0.0013 seconds
4, load the file execution time using the require_once () function
Solution: The average execution time for loading files using the require_once () function is 0.0011-0.0012 seconds
By the above test results, four function load file execution time is basically similar, the difference is that if there is a file multiple loading, the include and require functions are loaded multiple times, and the include_once and require_once functions will only be loaded once. While handling load failures differently, include () and include_once () produce a warning and require () and require_once () cause a fatal error.
Related instructions
Include () and require () functions
These two structures are exactly the same in addition to how they handle failures. The include () generates a warning and require () causes a fatal error. In other words, if you want to stop processing a page when you encounter a lost file, use require (). The include () is not the case and the script will continue to run. Also be sure to set the appropriate include_path. Note Before PHP 4.3.5, a syntax error in the include file does not cause the program to stop, but from this version.
The order in which to find the included files is looked for in the relative include_path of the current working directory, and then include_path under the current directory where the script is currently running. For example Include_path is., the current working directory is/www/, in the script to include a include/a.php and in the file has an include "b.php", the search b.php order first/www/, then/www/ include/. If the file name is./or: /start, search only under the include_path of the current working directory.
When a file is contained, the code contained in it inherits the scope of the variable for the row in which the include is located. From there, any variables that are available to the calling file at that line are also available in the called file. However, all functions and classes defined in the include file have 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, except that if the code in the file is already contained, it will not be included again. As the name of this statement implies, it is included only once. Include_once () and require_once () should be used when the same file is likely to be included more than once during script execution, to ensure that it is included only once to avoid problems such as function redefinition, variable re-assignment, and so on.
http://www.bkjia.com/PHPjc/444722.html www.bkjia.com true http://www.bkjia.com/PHPjc/444722.html techarticle The article uses the graphic detailed introduction in PHP about include and include_once and require and require_once Performance Map, the need for friends can be detailed to see, but overall include_once and ...