Require_once has poor performance.

Source: Internet
Author: User

After testing, require_once is a syntax structure with low performance. Of course, this low performance is relative to require. This article describes the require method currently used in our project and demonstrates its efficiency through the experiment code, at the same time, describe the problems we encounter during the use process to prevent others from tripping over the same stone.

  • Require: Introduce a file and compile and introduce it during runtime.
  • Require_once: The function is equivalent to require, but after the file is referenced, It is not compiled and introduced.

The above is the difference between the two. It can be seen that the difference between the two is that require_once has a mechanism to determine whether a reference has been made. By searching through the network, we can see a lot of data about require_once performance much lower than require, so we will not do this experiment here.

In our project, we define a global variable at the starting position of each file. When require, we use isset ($ xxxxxx) or require 'xxxxx. php ';

What are the shortcomings of this approach?

When a global variable is defined as $ xxx, if the file is require in the function, the variable will be resolved as a local variable of the function, rather than a global variable. Therefore, isset ($ xxx) or require 'xxx. php' the syntax structure will be invalid, and the results will certainly be unexpected, such as class redefinition and method redefinition.

For details about global variables, use $ GLOBALS ['xxx']. When require is used, use isset ($ GLOBALS ['xxx']) or require 'xxx. php ';, using GLOBALS is a little slower than defining it directly, but it is much better than making a mistake.

Since our previous global variables are directly defined, we thought of another method during our discussions with our colleagues today:

The location of the definition is still directly defined using the $ xxx method, and is modified in the require method (the global variables defined in the file header are associated with the file name ).

function ud_require($xxx) {global $$xxx;    isset($$xxx) or require $xxx . '.php';}

This method uses dynamic variables. After comparison with the direct GLOBALS method, there are two notable disadvantages:

  1. Performance. Due to the introduction of dynamic variables, it is about twice slower than the GLOBALS method.
  2. Indirect references cannot be solved because we cannot predict the file names that are indirectly referenced, so we cannot use global to declare the labeled global variables defined in indirectly referenced files.

Well, the following is my testing of require and require_once in GLOBALS mode:

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 "The isset or require method is not used :". ($ end-$ start ). "<br/>/ N "; $ start = microtime (true); while ($ j ++ <1000000) test1 ('require _ requireonce_require.php '); $ end = microtime (true ); echo "How to Use isset or require :". ($ end-$ start ). "<br/>/n"; $ start = microtime (true); while ($ k ++ <1000000) test2 (); $ end = microtime (true ); echo "require_once method :". ($ end-$ start ). "<br/>/n";?>

Require_requireonce_require.php (used to test the imported file of require)

<?php  $GLOBALS['require_requireonce_require.php'] = 1;  class T1 {}  ?>  

Require_requireonce_requireonce.php (used to test the imported file of require_once)

<?php  class T2 {}  ?>  

The following is the test result (unit: seconds ):

  • The isset or require method is not used: 0.22953701019287
  • How to Use isset or require: 0.23866105079651
  • Require_once method: 2.3119640350342

It can be seen that the require speed of another method is slightly faster than that of the method used, and the speed of both is about 10 times of require_once.

So where is the performance loss?

In the test1 method in the above require_requireone.php file, I commented a pathinfo ($ filename), Because I originally intended to use the name of the global variable without a suffix as the mark,, after using pathinfo, I found that the performance consumption of this method is basically the same as that of require_once. Therefore, I added a pathinfo call and tested it separately. It turns out that pathinfo is playing tricks. Therefore, I will change it to the current version and use the file name as the variable name. If you are afraid of repeated file names, add the path name...

Guess: After pathinfo is added, the performance consumption of require and require_once is basically the same. Can we guess that PHP's internal processing of require_once is based on it? It is said that PHP5.3 has made significant improvements to require_once. However, I used PHP5.3.5 during the test, but I still can see a significant gap with require. Is it just a big optimization than the previous version? This is not tested yet ....

Try to modify the test1 method as follows: isset ($ GLOBALS [substr ($ filename, 0, strlen ($ filename)-4)]) or require $ filename;

Using manual string truncation is time-consuming, but it is better than the pathinfo version. The result of this test is:

  • The isset or require method is not used: 0.21035599708557
  • How to Use isset or require: 0.92985796928406
  • Require_once method: 2.3799331188202

When you change require_once to isset or require, pay attention to the following aspects:

  1. Each file header defines a unique tag variable, which is defined in the format of $ GLOBALS ['xxx'] = 1; and, it is recommended that the variable name be a file name or a file name with a path (if the file name is unique)
  2. Define a custom require method:
  3. function ud_require_once($filename) {isset($GLOBALS[$filename]) or require $filename;}

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.