提高define性能的php擴(kuò)展hidef的安裝和使用
更新時(shí)間:2011年06月14日 21:38:43 作者:
在apache啟動(dòng)前,PHP啟動(dòng)時(shí)創(chuàng)建并初始化了這些常量,這樣就不需要在php里define常量了,性能自然沒有任何問題了!
官網(wǎng):http://pecl.php.net/package/hidef
簡(jiǎn)介:
Allow definition of user defined constants in simple ini files, which are then processed like internal constants, without any
of the usual performance penalties.
允許使用簡(jiǎn)單的ini文件來(lái)定義需要的常量,就像使用內(nèi)部變量一樣,而且沒有使用Define的性能問題。
作者說Hidef is initialized in php module init, before apache starts spawning children.
在apache啟動(dòng)前,PHP啟動(dòng)時(shí)創(chuàng)建并初始化了這些常量,這樣就不需要在php里define常量了,性能自然沒有任何問題了!
在Nginx下同樣可用,以下是安裝過程:
1、下載并解壓進(jìn)入目錄
# wget http://pecl.php.net/get/hidef-0.1.8.tgz
# tar zxvf hidef-0.1.8.tgz
# cd hidef-0.1.8
2、沒有configure文件,執(zhí)行phpize創(chuàng)建該文件
# /usr/local/webserver/php/bin/phpize
# ./configure --enable-hidef --with-php-config=/usr/local/webserver/php/bin/php-config
# make
# make install
3、添加到php.ini文件里面
# vi /usr/local/webserver/php/etc/php.ini
-----------------------------------------------
extension=hidef.so
hidef.ini_path=/usr/local/webserver/php/etc/
------------------------------------------------------------------------------
注意,如果php.ini文件里面沒有定義hidef.ini_path,則默認(rèn).ini文件讀取位置為/hidef,只需手工創(chuàng)建文件 vi /hidef/hidef.ini即可。
# vi /usr/local/webserver/php/etc/hidef.ini(此處根據(jù)情況自己調(diào)整路徑)
[hidef]
int ANSWER = 42;
str HX = "9enjoy";
float PIE = 3.14159;
這里整數(shù)用int,浮點(diǎn)數(shù)用float,字符串用str。
字符串str的值使用雙引號(hào)來(lái)包含,或者直接寫字符串內(nèi)容。如果使用單引號(hào),將會(huì)把單引號(hào)也做為字符串的內(nèi)容。
如str HX='9enjoy',實(shí)際存儲(chǔ)的不是9enjoy,是'9enjoy'。
4、重新加載php-fpm即可
# /usr/local/webserver/php/sbin/php-fpm reload
此時(shí),查看phpinfo()的結(jié)果,在hidef處就可以看到定義的變量。

-----------------------------------------------------------------------------
附:
如果使用了APC,apc提供了定義常量的方法。apc_define_constants和apc_load_constants。apc_define_constants將常量轉(zhuǎn)為數(shù)組存到一個(gè)user cache中。雖然把常量存在了內(nèi)存中,但每次PHP請(qǐng)求時(shí),仍然需要讀cache,分別定義,因此也不會(huì)有什么明顯的性能提升。我測(cè)試了下定義25個(gè)常量,使用apc的函數(shù)比直接定義常量快了0.01ms。
這樣使用:
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-performing solution, try the hidef extension from PECL.
APC的文檔中推薦使用hidef。
簡(jiǎn)介:
Allow definition of user defined constants in simple ini files, which are then processed like internal constants, without any
of the usual performance penalties.
允許使用簡(jiǎn)單的ini文件來(lái)定義需要的常量,就像使用內(nèi)部變量一樣,而且沒有使用Define的性能問題。
作者說Hidef is initialized in php module init, before apache starts spawning children.
在apache啟動(dòng)前,PHP啟動(dòng)時(shí)創(chuàng)建并初始化了這些常量,這樣就不需要在php里define常量了,性能自然沒有任何問題了!
在Nginx下同樣可用,以下是安裝過程:
1、下載并解壓進(jìn)入目錄
# wget http://pecl.php.net/get/hidef-0.1.8.tgz
# tar zxvf hidef-0.1.8.tgz
# cd hidef-0.1.8
2、沒有configure文件,執(zhí)行phpize創(chuàng)建該文件
# /usr/local/webserver/php/bin/phpize
# ./configure --enable-hidef --with-php-config=/usr/local/webserver/php/bin/php-config
# make
# make install
3、添加到php.ini文件里面
# vi /usr/local/webserver/php/etc/php.ini
-----------------------------------------------
extension=hidef.so
hidef.ini_path=/usr/local/webserver/php/etc/
------------------------------------------------------------------------------
注意,如果php.ini文件里面沒有定義hidef.ini_path,則默認(rèn).ini文件讀取位置為/hidef,只需手工創(chuàng)建文件 vi /hidef/hidef.ini即可。
# vi /usr/local/webserver/php/etc/hidef.ini(此處根據(jù)情況自己調(diào)整路徑)
復(fù)制代碼 代碼如下:
[hidef]
int ANSWER = 42;
str HX = "9enjoy";
float PIE = 3.14159;
這里整數(shù)用int,浮點(diǎn)數(shù)用float,字符串用str。
字符串str的值使用雙引號(hào)來(lái)包含,或者直接寫字符串內(nèi)容。如果使用單引號(hào),將會(huì)把單引號(hào)也做為字符串的內(nèi)容。
如str HX='9enjoy',實(shí)際存儲(chǔ)的不是9enjoy,是'9enjoy'。
4、重新加載php-fpm即可
# /usr/local/webserver/php/sbin/php-fpm reload
此時(shí),查看phpinfo()的結(jié)果,在hidef處就可以看到定義的變量。

-----------------------------------------------------------------------------
附:
如果使用了APC,apc提供了定義常量的方法。apc_define_constants和apc_load_constants。apc_define_constants將常量轉(zhuǎn)為數(shù)組存到一個(gè)user cache中。雖然把常量存在了內(nèi)存中,但每次PHP請(qǐng)求時(shí),仍然需要讀cache,分別定義,因此也不會(huì)有什么明顯的性能提升。我測(cè)試了下定義25個(gè)常量,使用apc的函數(shù)比直接定義常量快了0.01ms。
這樣使用:
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-performing solution, try the hidef extension from PECL.
APC的文檔中推薦使用hidef。
相關(guān)文章
CMSPRESS 10行代碼搞定 PHP無(wú)限級(jí)分類2
超級(jí)無(wú)限分類 使用簡(jiǎn)單 效率極高 核心代碼10行不到,另外 求這個(gè)分類的不足,和更高效簡(jiǎn)單的無(wú)限分類方法2018-03-03PHP下SSL加密解密、驗(yàn)證、簽名方法(很簡(jiǎn)單)
這篇文章主要介紹了PHP下SSL加密解密、驗(yàn)證、簽名方法,方法很簡(jiǎn)單,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2015-12-12JS+PHP實(shí)現(xiàn)用戶輸入數(shù)字后顯示最大的值及所在位置
這篇文章主要給大家介紹了JS+PHP實(shí)現(xiàn)用戶輸入數(shù)字后顯示最大的值及是第幾個(gè)的相關(guān)位置,文中給出了詳細(xì)的示例代碼供大家參考學(xué)習(xí),需要的朋友們下面來(lái)一起看看吧。2017-06-06解析PHP中數(shù)組元素升序、降序以及重新排序的函數(shù)
本篇文章是對(duì)PHP中數(shù)組元素升序、降序以及重新排序的函數(shù)進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06使用PHP獲取兩個(gè)數(shù)組之間的差集、并集、交集方法
這篇文章主要給大家介紹了使用PHP獲取兩個(gè)數(shù)組之間的差集、并集、交集方法,文章通過代碼示例介紹的非常詳細(xì),具有一定的參考價(jià)值,需要的朋友可以參考下2023-11-11淺談ThinkPHP5.0版本和ThinkPHP3.2版本的區(qū)別
小編小編就為大家?guī)?lái)一篇淺談ThinkPHP5.0版本和ThinkPHP3.2版本的區(qū)別。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧2017-06-06