php inlcude of performance comparison details

  • 2020-05-24 05:15:26
  • OfStack

include performance
 
include('include.php'); 


There is nothing wrong with this approach, but it is slightly less efficient than the following:

 
include(realpath(dirname(_FILE_)).DIRECTORY_SEPARATOR.'include.php'); 


This way we may have to type more than 1, but instead of having the PHP engine iteratively find all the objects with the name 'include.php' in include_path, the absolute path designation of dirname(s 16en__) will allow the system to quickly locate the corresponding file.

The constant s s 20en__ in PHP is very similar to AppDomain.CurrentDomain.BaseDirectory in C#, which returns the absolute path of the file in which the current code is executing. The function dirname() returns the path to its parent folder.
Another more efficient and easy to write method is include('./ include.php '), which tells the system to find the 'include.php' file in the current path.

In large systems we often use another, better approach. We often add the following code to the routing file or other initialization file:

 
define('APP_PATH',realpath(dirname(_FILE_))); 

This is equivalent to adding a global variable to the system to indicate the root directory of the system. When we need to refer to a file in a specific path later, we can use the following code:
 
include(APP_PATH.DIRECTORY_SEPARATOR.'models'.'User.php'); 


Performance comparison between autoload and include

For example, there are four scripts:

 
#file:include1.php 
include 'include2.php'; 
//@todo something#file:include2.php 
//@todo something#file:script1.php 
include 'include2.php'; 
//@todo something 
#file:script2.php 
include 'include1.php'; 
include 'script1.php' 
//@todo something 

When script1.php is executed, include 'include2.php '; This line of code is executed once. When script2.php is executed, this line of code is executed twice.
Here is just a simple example. In the actual project, include2.php may be include more times. Does this repetition of include affect performance? I wrote a script to test it.

 
#file:SimpleClass.php 
class SimpleClass { 
public function __construct() { 
echo get_time() . "rn"; 
} 
} 
#file:php_include.php 
for($i = 0;$i < $loop;$i++) { 
include_once "SimpleClass.php"; 
new SimpleClass(); 
} 


When the value of $loop is 1, the script takes about 0.00018906593322754 seconds, and when the value of $loop is 1000, the script takes about 0.076701879501343 seconds.

What if we implement it with autoload?

 
#file:php_autoload.php 
function __autoload($class_name) { 
include_once $class_name . '.php'; 
}for($i = 0;$i < $loop;$i++) { 
new SimpleClass(); 
} 

In this code, I define a script with almost 1 type of function, which takes 0.0002131462097168 seconds when $loop is 1, and 1/7 of 0.012391805648804 seconds when $loop is 1000.
But notice the code for SimpleClass, which outputs 1 line of string. What happens when you subtract this line of output and compare it?

When $loop is the same as 1000, it takes 0.057836055755615 seconds, while when autoload is used, it only takes 0.00199294090271 seconds! Efficiency difference nearly 30 times!

As you can see from the above test, when the file is only include1 times, autoload will consume a little more than 1 point of time, but if the file is repeatedly include, using autoload can greatly improve the system performance.
Whether or not to use autoload to free up programmers is a matter of opinion. In my opinion, it is worth sacrificing this one point of performance (and in some cases, perhaps even improving performance) for easier development when conditions permit.

include() and require() performance

For include(), the file is read and evaluated each time include() is executed;
For require(), the file is processed only once (actually, the content of the file replaces the require() statement).
This means that it is more efficient to use require() if you have code that contains one of these instructions and code that may be executed multiple times.
On the other hand, include() is used if a different file is read each time the code is executed, or if there is a loop through a stack of files,
Because you can set a variable for the file name you want to include, you can use this variable when the parameter is include().

Related articles: