欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

提高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)整路徑)
復(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)文章

最新評(píng)論