程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> PHP綜合 >> 提高define性能的php擴展hidef的安裝和使用

提高define性能的php擴展hidef的安裝和使用

編輯:PHP綜合
官網:http://pecl.php.net/package/hidef
簡介:
  Allow definition of user defined constants in simple ini files, which are then processed like internal constants, without any
of the usual performance penalties.
  允許使用簡單的ini文件來定義需要的常量,就像使用內部變量一樣,而且沒有使用Define的性能問題。

作者說Hidef is initialized in php module init, before apache starts spawning children.
在apache啟動前,PHP啟動時創建並初始化了這些常量,這樣就不需要在php裡define常量了,性能自然沒有任何問題了!
在Nginx下同樣可用,以下是安裝過程:

1、下載並解壓進入目錄

# 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文件,執行phpize創建該文件

# /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,則默認.ini文件讀取位置為/hidef,只需手工創建文件 vi /hidef/hidef.ini即可。

# vi /usr/local/webserver/php/etc/hidef.ini(此處根據情況自己調整路徑)
復制代碼 代碼如下:
[hidef]
int ANSWER = 42;
str HX = "9enjoy";
float PIE = 3.14159;

這裡整數用int,浮點數用float,字符串用str。
字符串str的值使用雙引號來包含,或者直接寫字符串內容。如果使用單引號,將會把單引號也做為字符串的內容。
如str HX='9enjoy',實際存儲的不是9enjoy,是'9enjoy'。

4、重新加載php-fpm即可

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

此時,查看phpinfo()的結果,在hidef處就可以看到定義的變量。


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

附:

如果使用了APC,apc提供了定義常量的方法。apc_define_constants和apc_load_constants。apc_define_constants將常量轉為數組存到一個user cache中。雖然把常量存在了內存中,但每次PHP請求時,仍然需要讀cache,分別定義,因此也不會有什麼明顯的性能提升。我測試了下定義25個常量,使用apc的函數比直接定義常量快了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。
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved