Install and use the php extension hidef to improve define performance

Source: Internet
Author: User
Tags apc constant definition

Official Website: http://pecl.php.net/package/hidef
Introduction:
Allow definition of user defined constants in simple ini files, which are then processed like internal constants, without any
Of the usual performance penalties.
Allow the use of a simple INI file to Define the required constants, just like using internal variables, without the performance issue of using Define.

The author said Hidef is initialized in php module init, before apache starts spawning children.
Before apache is started, PHP creates and initializes these constants at startup, so you do not need to define the constants in php. The performance is naturally no problem!
The installation process is as follows:

1. Download and decompress the package to the directory.

# Wget http://pecl.php.net/get/hidef-0.1.8.tgz
# Tar zxvf hidef-0.1.8.tgz
# Cd hidef-0.1.8

2. If no configure file exists, run phpize to create the file.

#/Usr/local/webserver/php/bin/phpize
#./Configure -- enable-hidef -- with-php-config =/usr/local/webserver/php/bin/php-config
# Make
# Make install

3. Add it to the php. ini file.

# Vi/usr/local/webserver/php/etc/php. ini

-----------------------------------------------
Extension = hidef. so
Hidef. ini_path =/usr/local/webserver/php/etc/
------------------------------------------------------------------------------

NOTE: If hidef. ini_path is not defined in the php. ini file, the default read location of the. ini file is/hidef. You only need to manually create the file vi/hidef. ini.

# Vi/usr/local/webserver/php/etc/hidef. ini (adjust the path as needed)
Copy codeThe Code is as follows:
[Hidef]
Int ANSWER = 42;
Str HX = "9 enjoy ";
Float PIE = 3.14159;

Here, the integer is int, the floating point is float, and the string is str.
The string 'str' value is enclosed in double quotation marks, or the string content is directly written. If you use single quotes, the single quotes are also used as the content of the string.
For example, if str HX = '9enjoy ', the actual storage is not 9 enjoy, but '9enjoy '.

4. Reload php-fpm.

#/Usr/local/webserver/php/sbin/php-fpm reload

View the phpinfo () result, and you can see the defined variable at hidef.


-----------------------------------------------------------------------------

Appendix:

If APC is used, apc provides a method for defining constants. Apc_define_constants and apc_load_constants. Apc_define_constants converts constants into arrays and stores them in a user cache. Although constants are stored in the memory, each PHP request still needs to read the cache and define them separately, so there will be no significant performance improvement. I tested the definition of 25 constants, and the use of apc functions is 0.01 ms faster than the direct definition of constants.

This method is used as follows:
If (! Apc_load_constants ('defined ')){
$ Constants = array (
'Hx '=> TRUE,
'D _ BUG '=> 1
);
Apc_define_constants ('defined', $ constants );
}

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. however, this function does not perform as well as anticipated.

For a better-inform Ming solution, try the hidef extension from PECL.

Hidef is recommended in APC documents.

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.