Require_once's performance is actually very low _php tutorial

Source: Internet
Author: User
After testing, require_once is a poor performance of the grammatical structure, of course, this low performance is relative to require, this article describes our project currently used in the require way, through the experimental code to prove its efficiency, while describing our use process encountered problems, Prevent others from tripping on the same stone.

    • Require: Introduces a file, run-time compilation introduced.
    • Require_once: The function is equivalent to require, but when the file is referenced, it is no longer compiled.

Above is the difference between the two. It can be seen that the difference between the two is that require_once has a mechanism for judging whether it has been quoted. With the web search, you can see a lot of data about the require_once performance is much lower than the require, this experiment is no longer done here.

The practice in our project is to define a global variable at the start of each file, using Isset ($xxxxxx) or require ' xxxxx.php ' when require;

What is the disadvantage of this approach?

When a global variable is defined in a $xxx manner, if the file is require within a function, the variable is parsed as a local variable of the function, not a global one, so the Isset ($xxx) or require ' xxx.php ' syntax structure inside the function is invalidated. The results are certainly unexpected, such as the redefinition of the class, the redefinition of the method, and so on.

As a cautionary tale, so, the definition of global variables, please use $globals[' xxx '],require when using isset ($GLOBALS [' xxx ']) or require ' xxx.php ';, using GLOBALS is slightly slower than the direct definition, But it's better than wrong.

Since our previous global variables are directly defined, today in the process of discussing with colleagues, think of another way of writing:

The defined location is still defined directly using the $xxx method, which is modified in require (the global variable and file name defined by the header are associated).

function Ud_require ($xxx) {global $ $xxx;    Isset ($ $xxx) or require $xxx. '. php ';}

This approach uses dynamic variables, which are compared with the direct Globals method, with two notable drawbacks:

    1. Performance, due to the introduction of dynamic variables, twice times slower than the Globals mode.
    2. The indirect reference problem cannot be solved because we cannot predict the names of the indirectly referenced files, nor can we use global to declare the tagged global variables defined in the indirectly referenced files.

OK, here's my test of the require and require_once of the Globals way:

require_requireonce.php

 
  /n ";  $start = Microtime (true);  while ($j + + < 1000000) test1 (' require_requireonce_require.php ');  $end = Microtime (true);  echo "isset or require way of using method:". ($end-$start). "
/n "; $start = Microtime (true); while ($k + + < 1000000) test2 (); $end = Microtime (true); echo "Require_once way:". ($end-$start). "
/n "; ? >

require_requireonce_require.php (used to test require of the introduced files)

 
    

require_requireonce_requireonce.php (used to test require_once of the introduced files)

 
    

The following is the result of the test (in seconds):

    • Isset or require mode without using the method: 0.22953701019287
    • Isset or require method of use: 0.23866105079651
    • require_once Mode: 2.3119640350342

It can be seen that the require speed of a method is slightly faster than the use of the method, both of which are about 10 times times the speed of require_once.

So where is the performance loss?

In the Test1 method in the require_requireone.php file above, I commented on a pathinfo ($filename), because I intended to use a filename without a suffix as a marked global variable name, but, When I used pathinfo, I found that the performance cost of this approach was basically consistent with require_once. So, I was there alone added a pathinfo call, and did a test, sure enough is pathinfo in mischief. So, in the future I will modify for the current version, directly using the file name as the variable name, if you are afraid of duplicate file name, it may be added path name ...

Guess: After adding pathinfo, require and require_once performance consumption is basically consistent, then we can guess PHP internal processing of the require_once is based on it? It is said that the require_once has been significantly optimized in the PHP5.3, but I am using the PHP5.3.5 version of the test process, can still see and require the obvious gap, is it just larger than the previous version of the optimization? 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;

Using manual string Interception is, of course, time-consuming to intercept, but better than the PathInfo version. The results of this test are:

    • Isset or require mode without using the method: 0.21035599708557
    • Isset or require method of use: 0.92985796928406
    • require_once Mode: 2.3799331188202

For require_once to be modified to isset or require, you need to be aware of the following:

    1. Each file header defines a unique tag variable, using $globals[' XXX '] = 1, and the suggested variable name is the filename or file name with the path (if a separate file name repeats)
    2. Define a custom require method:
    3. function Ud_require_once ($filename) {isset ($GLOBALS [$filename]) or require $filename;}

http://www.bkjia.com/PHPjc/752440.html www.bkjia.com true http://www.bkjia.com/PHPjc/752440.html techarticle after testing, require_once is a poor performance of the grammatical structure, of course, this low performance is relative to the require, this article describes our project is currently used in the Require way, through ...

  • 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.