APC constant definition vs. PHP define comparison _php Tutorial

Source: Internet
Author: User
Tags apc constant definition define function php define
Recently in the cloud Platform for the initial code architecture, encountered a constant definition speed comparison problem, so do a comparison.
The APC extension for PHP is described in the following paragraph 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 was provided to stream Line the process of mass constant definition.
This means that PHP's define function is slow, and in an APC-enabled PHP environment, the constant definition of APC is much faster than the define.
The APC constants define the pair of functions using apc_define_constants () and apc_load_constants ().
Here are two procedures, respectively, to test their running time to see the difference:
Code for the Define function:
$stime =microtime (TRUE);

Define (' Tmp_path ', '/tmp ');
// ... Other definitions, total 20

Echo Api_mail;
Echo '
';

$etime =microtime (TRUE);
Echo $etime-$stime;
?>
The constant definition code for APC:
$stime =microtime (TRUE);
if (!apc_load_constants (' API ')) {
Apc_define_constants (' API ', Array (
' Tmp_path ' = '/tmp ',
// ... Other definitions, total 20
));
}

Echo Api_mail;
Echo '
';

$etime =microtime (TRUE);
Echo $etime-$stime;
?>
Execution Result:
Define function:
0.000098943710327148
0.00010895729064941
0.00010585784912109
0.00010395050048828
...
APC constants are defined as:
0.00010991096496582
0.000039100646972656
0.000042915344238281
0.000041961669921875
...
As can be seen from the results, the APC constants are defined to take the same amount of time and define as the first execution, but after the first execution, the execution time is very low, only the define One-third. and define execution time, each time is very average, and does not have the big fluctuation.
From the Code analysis, the APC constant definition is to get 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 benefit is that the constants are imported into PHP execution space at once, and do not need to be define each time, so it is more efficient.
Note: In this test, the PHP environment turns on the APC cache, so the test of the Define function is also run at the memory level.


This article is from the "technical Notepad in the vibration" blog

http://www.bkjia.com/PHPjc/478689.html www.bkjia.com true http://www.bkjia.com/PHPjc/478689.html techarticle recently in the cloud Platform for the initial code architecture, encountered a constant definition speed comparison problem, so do a comparison. The APC extension for PHP is described in the following paragraph in the PHP manual: H ...

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