Comparison of APC constant definition and PHP define_PHP tutorial

WBOY
Release: 2016-07-13 17:45:08
Original
1237 people have browsed it

Recently, when I was working on the preliminary code architecture of the cloud platform, I encountered a problem of constant definition speed comparison, so I made a comparison.
The APC extension of PHP has the following description in the PHP manual:
http://cn.php.net/manual/zh/function.apc-define-constants.php
define() is notoriously slow. Since the main benefit of APC is to increase the performance of scripts/applications, this mechanism is provided to streamline the process of mass constant definition.
This means that PHP's define function is relatively slow. In a PHP environment with apc enabled, using apc's constant definition method is much faster than define.
Apc constant definition uses the pair of functions apc_define_constants() and apc_load_constants().
Two programs are prepared here, and the running time is tested separately to see the difference:
Code of define function:
$stime=microtime(true);

define('TMP_PATH', '/tmp');
// ...Other definitions, 20 in total

echo API_MAIL;
echo '
';

$etime=microtime(true);
echo $etime-$stime;
?>
apc constant definition code:
$stime=microtime(true);
if(!apc_load_constants('API')){
apc_define_constants('API', array(
'TMP_PATH' => '/tmp',
// ... other definitions, 20
));
}

echo API_MAIL;
echo '
';

$etime=microtime(true);
echo $etime-$stime;
?>
Execution result:
define function:
0.000098943710327148
0.00010895729064941
0.00010585784912109
0.00010395050048828
...
apc constant definition:
0.00010991096496582
0.000039100646972656
0.000042915344238281
0.000041961669921875
...
It can be seen from the results that when the apc constant definition is executed for the first time, it takes about the same time as define; but after the first execution, the subsequent execution time is very small, only one-third of define. The execution time of define is average every time and does not fluctuate much.
From the code analysis, the apc constant definition is to first obtain the constant through the apc_load_constants() function, and then execute apc_define_constants() to define the constant when the constant does not exist. The advantage of this is that the constants are imported into the PHP execution space at once, and there is no need to define each one, so it is more efficient.
Note: In this test, the PHP environment has apc cache turned on, so the test of the define function is also run at the memory level.


This article is from the "Zhenzhong's Technical Notepad" blog

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/478689.htmlTechArticleRecently when doing the preliminary code architecture of the cloud platform, I encountered a problem of constant definition speed comparison, so I decided to do it Compare. The APC extension of PHP has the following description in the PHP manual: h...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!