Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Performance comparison of inlcude () and require () in php

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the knowledge of "performance comparison between inlcude () and require () in php". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Include performance

The copy code is as follows:

Include ('include.php')

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

The copy code is as follows:

Include (realpath (dirname (_ FILE_)) .DIRECTORY_SEPARATOR.'include.php')

In this way, we may need to type a little more, but compared to the previous one that requires the PHP engine to iterate through the include_path to find all the objects with the name 'include.php' to find the corresponding objects, the absolute path dirname (_ _ FILE__) will allow the system to quickly locate the corresponding files.

The constant _ _ FILE__ in PHP is actually very similar to AppDomain.CurrentDomain.BaseDirectory in C# in that it returns the absolute path to the file where the current code is executing. The function dirname () returns its parent folder path.

Another more efficient and easy way to write is include ('. / include.php'), which is tantamount to telling the system to find the 'include.php' file under the current path.

We often use another better approach in large systems, and we often add the following code to routing files or other initialization files:

The copy code is as follows:

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 reference a file under a specific path later, we can use the following code:

The copy code is as follows:

Include (APP_PATH.DIRECTORY_SEPARATOR.'models'.'User.php')

Performance comparison between autoload and include

For example, there are four scripts:

The copy code is as follows:

# 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, the line of include 'include2.php'; 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. Will such repeated include affect performance? So I wrote a script to test it.

The copy code is as follows:

# 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 $loop is 1, the script takes about 0.00018906593322754 seconds, and when $loop is 1000, the script takes about 0.076701879501343 seconds.

What if we implement it in autoload?

The copy code is as follows:

# file:php_autoload.php

Function _ _ autoload ($class_name) {

Include_once $class_name. '.php'

} for ($I = 0bot / I < $loop;$i++) {

New SimpleClass ()

}

In this code, I define the _ _ autoload function, which is almost the same script. When $loop is 1, it takes 0.0002131462097168 seconds, but when $loop is 1000, it takes only 0.012391805648804 seconds of the previous code.

But notice the SimpleClass code, which outputs a line of strings. What would be the result if you removed this line of output and compared it?

When $loop is the same as 1000, the former takes 0.057836055755615 seconds, but only 0.00199294090271 seconds after using autoload! The difference in efficiency is nearly 30 times!

As you can see from the above tests, when the file is include only once, autoload consumes a little more time, but if the file is repeatedly include, using autoload can greatly improve system performance.

As to whether or not to use autoload to liberate programmers, this is a matter of opinion and wisdom. In my opinion, if conditions permit, it is worth sacrificing this performance (and in some cases, perhaps even improving performance) for more convenient development.

Include () and require () performance

For include (), the file is read and evaluated every time include () is executed

In the case of require (), the file is processed only once (in fact, the file content 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 that may be executed multiple times.

On the other hand, if you read a different file each time the code is executed, or if there is a loop that iterates through a set of files, use include ()

Because you can set a variable for the file name you want to include, use this variable when the parameter is include ().

That's all for "performance comparison between inlcude () and require () in php". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report