Home > Article > Backend Development > The performance of require_once is actually very low_PHP tutorial
After testing, require_once is a syntax structure with low performance. Of course, this low performance is relative to require. This article explains the require method currently used in our project and proves its efficiency through experimental code. At the same time , describe the problems we encountered during use, and avoid others stumbling on the same stone.
The above is the difference between the two. It can be seen that the only difference between the two is that require_once has a mechanism to determine whether it has been referenced. Through Internet searches, you can see a lot of data about the performance of require_once being much lower than require. This experiment will not be done here.
The approach in our project is: Define a global variable at the beginning of each file. When require, use isset($xxxxxx) or require 'xxxxx.php';
What are the disadvantages of this approach?
When a global variable is defined in $xxx, if the file is required within the function, the variable will be parsed as a local variable of the function instead of global. Therefore, isset($xxx) or require inside the function The syntactic structure of 'xxx.php' will be invalid, and the results will be unexpected, such as class redefinition, method redefinition, etc.
Learning from the past, so to define global variables, please use $GLOBALS['xxx']. When requiring, use isset($GLOBALS['xxx']) or require 'xxx.php';. Using GLOBALS will be better than Direct definition is a little slower, but it's much better than being wrong.
Since our previous global variables were directly defined, during the discussion with colleagues today, another way of writing came to mind:
The defined position is still defined directly using the $xxx method, and modified in the require method (the global variables defined in the file header are related to the file name).
function ud_require($xxx) { global $$xxx; isset($$xxx) or require $xxx . '.php'; }
This method uses dynamic variables. Compared with the direct GLOBALS method, it has two significant disadvantages:
Okay, here is my test of require and require_once in GLOBALS method:
require_requireonce.php
<?php function test1($filename) { //pathinfo($filename); isset($filename) or require $filename; } function test2() { require_once 'require_requireonce_requireonce.php'; } $start = microtime(true); while($i ++ < 1000000) isset($GLOBALS['require_requireonce_require.php']) or require 'require_requireonce_require.php'; $end = microtime(true); echo "不使用方法的isset or require方式: " . ($end - $start) . "<br />/n"; $start = microtime(true); while($j ++ < 1000000) test1('require_requireonce_require.php'); $end = microtime(true); echo "使用方法的isset or require方式: " . ($end - $start) . "<br />/n"; $start = microtime(true); while($k ++ < 1000000) test2(); $end = microtime(true); echo "require_once方式: " . ($end - $start) . "<br />/n"; ?>
require_requireonce_require.php (introduced file used to test require)
<?php $GLOBALS['require_requireonce_require.php'] = 1; class T1 {} ?>
require_requireonce_requireonce.php (introduced file used to test require_once)
<?php class T2 {} ?>
The following are the test results (unit: seconds):
It can be seen that the require speed without a method is slightly faster than that using the method. Both speeds are about 10 times that of require_once.
So, where is the performance loss?
In the test1 method in the require_requireone.php file above, I commented pathinfo($filename), because my original intention was to use the file name without a suffix as a marked global variable name, but when I use After pathinfo, I found that the performance consumption of this method is basically the same as require_once. Therefore, I added a separate call to pathinfo there and did a test. Sure enough, pathinfo was causing trouble. Therefore, I later modified it to the current version and directly used the file name as the variable name. If you are afraid of duplicate file names, you might as well add the path name...
Guess: After adding pathinfo, the performance consumption of require and require_once is basically the same. So can we guess that PHP's internal processing of require_once is based on it? It is said that require_once has been significantly optimized in PHP5.3. However, I used the PHP5.3.5 version during the test, and I can still see the obvious gap with require. Is it just a larger optimization than the previous version? This has not been tested yet....
Try to modify the test1 method as follows: isset($GLOBALS[substr($filename, 0, strlen($filename) - 4)]) or require $filename;
Use manual string interception. Of course, interception is time-consuming, but it is better than the pathinfo version. The test result this time is:
When changing require_once to isset or require mode, you need to pay attention to the following aspects:
function ud_require_once($filename) { isset($GLOBALS[$filename]) or require $filename; }