Compare the efficiency of the two methods of loading files with PHP include

  • 2020-03-31 21:07:37
  • OfStack

Here are two ways:
1) define a string variable that holds a list of files to load. Then the foreach loads.
 
$a = '/a.class.php;/Util/b.class.php;/Util/c.class.php'; 
$b = '/d.php;/e.class.php;/f.class.php;/g.class.php'; 
//Load the base system file
$kernel_require_files = explode(';', $a);//SYS_REQUIRE_LIB_FILE_LIST); 
foreach($kernel_require_files as $f){ 
require_once(SYS_LIB_PATH.'/System'.$f); 
} 

//Load the base system file
$kernel_require_files = explode(';', $b);//SYS_BASE_FILE_LIST); 
foreach($kernel_require_files as $f){ 
require_once(KERNEL_PATH.$f); 
} 

2) all the files to be loaded are loaded in an include file, and the current page directly includes this include file.
Include.php file contents
 
require_once('func.php'); 
require_once('LangManager.class.php'); 
require_once('_KernelAutoLoader.class.php'); 
require_once('ApplicationSettingManager.class.php'); 

require_once('lib/System/Activator.class.php'); 
require_once('lib/System/Util/CXML.class.php'); 
require_once('lib/System/Util/CWeb.class.php'); 

Personally, I think the second method is more efficient, because it doesn't have foreach or anything like that. Here is the time taken by 10 random loads using the two methods:
The foreach
0.017754077911377
0.017686128616333
0.017347097396851
0.018272161483765
0.018272161483765
0.018401145935059
0.018187046051025
0.020787000656128
0.018001079559326
0.017963171005249


Include. PHP include_once (' ');
0.025792121887207
0.024733066558838
0.025041103363037
0.024915933609009
0.024657011032104
0.024134159088135
0.025845050811768
0.024954080581665
0.024757146835327
0.02684497833252

In addition, I tried again to load all the files directly on the current page
0.022285938262939
0.024394035339355
0.023194074630737
0.023229122161865
0.024644136428833
0.023538112640381
0.024240016937256
0.025094032287598
0.023231029510498
0.02339506149292
The result surprised me! Actually the first method seems to be the slowest, the time is the least, and directly in the current page load multiple files time is also a lot of ah ~
The reason? Unknown ah, hope to give an answer to the obvious, let alone so many "X plan "core loading part of the first method

Related articles: