


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.
- require: Introduce a file and compile and import it at runtime.
- require_once: The function is equivalent to require, except that when this file is referenced, it will no longer be compiled and imported.
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:
- Performance, due to the introduction of dynamic variables, is about 2 times slower than the GLOBALS method.
- The indirect reference problem cannot be solved because we cannot predict the file name that is indirectly referenced, and we cannot use global to declare the marked global variables defined in the indirectly referenced files.
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):
- Isset or require method without using method: 0.22953701019287
- Use isset or require method: 0.23866105079651
- require_once method: 2.3119640350342
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:
- Isset or require method without using method: 0.21035599708557
- Use isset or require method: 0.92985796928406
- require_once method: 2.3799331188202
When changing require_once to isset or require mode, you need to pay attention to the following aspects:
- Each file header defines a unique tag variable, defined using $GLOBALS['XXX'] = 1;, and it is recommended that the variable name be the file name or a file name with a path (if a separate file Famous party repeats)
- Define a custom require method:
function ud_require_once($filename) { isset($GLOBALS[$filename]) or require $filename; }

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.
