關於PHP 擴充的幾個實作手段
1.php 原生擴充開發c 語言,附註:【ext_skel.php】腳本建立
##2.zephir 3.php-cpp4.php-x5.cgo圍繞【zephir,cgo,PHP 開啟 JIT】4 種模式下,透過斐波那契數列計算效能,來查看運行效果。
zephir 程式碼產生擴充功能//Main 类 final class Zimuge { public static function calcFibonacci(int i){ if (i < 2) { return i; } return self::calcFibonacci(i - 1) + self::calcFibonacci(i - 2); }
编译安装 zephir build
package main /* #ifdef HAVE_CONFIG_H #include "config.h" #endif #include "php.h" #include "php_ini.h" #include "ext/standard/info.h" static int le_go2php; PHP_MINIT_FUNCTION(go2php) { return SUCCESS; } PHP_MSHUTDOWN_FUNCTION(go2php) { return SUCCESS; } PHP_RINIT_FUNCTION(go2php) { return SUCCESS; } PHP_RSHUTDOWN_FUNCTION(go2php) { return SUCCESS; } PHP_MINFO_FUNCTION(go2php) { php_info_print_table_start(); php_info_print_table_header(2, "go2php support", "enabled"); php_info_print_table_end(); } PHP_FUNCTION(go2php_print) { zend_long a,b; ZEND_PARSE_PARAMETERS_START(1, 1) Z_PARAM_LONG(a) ZEND_PARSE_PARAMETERS_END(); b = calcFib(a); RETURN_LONG(b); } ZEND_BEGIN_ARG_INFO(null, 0) ZEND_END_ARG_INFO() const zend_function_entry go2php_functions[] = { PHP_FE(go2php_print, null) PHP_FE_END }; zend_module_entry go2php_module_entry = { STANDARD_MODULE_HEADER, "go2php", go2php_functions, PHP_MINIT(go2php), PHP_MSHUTDOWN(go2php), PHP_RINIT(go2php), PHP_RSHUTDOWN(go2php), PHP_MINFO(go2php), "0.1.0", STANDARD_MODULE_PROPERTIES }; #ifdef COMPILE_DL_GO2PHP ZEND_GET_MODULE(go2php) #endif */ import "C" func main() {}
package main import "C" //export calcFib func calcFib(i int) int { if i < 2 { return i } return calcFib(i-1)+calcFib(i-2) }
CGO_CFLAGS="-g \ -I`/root/download/php8/bin/php-config --include-dir` \ -I`/root/download/php8/bin/php-config --include-dir`/main \ -I`/root/download/php8/bin/php-config --include-dir`/TSRM \ -I`/root/download/php8/bin/php-config --include-dir`/Zend \ -I`/root/download/php8/bin/php-config --include-dir`/ext \ -I`/root/download/php8/bin/php-config --include-dir`/ext/date/lib \ -DHAVE_CONFIG_H" CGO_LDFLAGS="-Wl,--export-dynamic -Wl,--unresolved-symbols=ignore-all" go build -p 1 -gcflags "-l" -buildmode=c-shared -o go2php.so
<?php const COUNT = 30; function calcFibonacci(int $i): int { if ($i < 2) { return $i; } return calcFibonacci($i - 1) + calcFibonacci($i - 2); } // CGO 速度 $startTime = microtime(true); for($i = 1; $i <= COUNT; $i++) { if($i != COUNT) { go2php_print($i); }else { echo go2php_print($i)."\n"; } } $time = microtime(true) - $startTime; echo "CGO: {$time} 秒\n"; //zephir 速度 $startTime = microtime(true); for($i = 1; $i <= COUNT; $i++) { if($i != COUNT) { Lsz\Zimuge::calcFibonacci($i); }else { echo Lsz\Zimuge::calcFibonacci($i)."\n"; } } $time = microtime(true) - $startTime; echo "zephir: {$time} 秒\n"; // PHP JIT 速度 $startTime = microtime(true); for($i = 1; $i <= COUNT; $i++) { if($i != COUNT) { calcFibonacci($i); }else { echo calcFibonacci($i)."\n"; } } $time = microtime(true) - $startTime; echo "PHP: {$time} 秒\n";
php test.php ->执行结果取一个平均 832040 CGO: 0.059875011444092 秒 832040 zephir: 8.5679790973663 秒 832040 PHP: 0.75995492935181 秒
php -dopcache.enable_cli=1 -dopcache.jit_buffer_size=100M test.php ->执行结果取一个平均 832040 CGO: 0.046900987625122 秒 832040 zephir: 5.5882248878479 秒 832040 PHP: 0.10621190071106 秒
[PHP Modules] Core ctype curl date dom FFI fileinfo filter gd go2php hash iconv json libxml lsz mbstring mysqlnd openssl pcre PDO pdo_mysql pdo_sqlite Phar posix redis Reflection session SimpleXML SPL sqlite3 standard swoole tokenizer xml xmlreader xmlwriter yaf Zend OPcache zephir_parser zimuge [Zend Modules] Zend OPcache
php -v PHP 8.1.3 (cli) (built: Feb 27 2022 19:40:08) (NTS) Copyright (c) The PHP Group Zend Engine v4.1.3, Copyright (c) Zend Technologies with Zend OPcache v8.1.3, Copyright (c), by Zend Technologies
結論:
JIT 能提升 php 的效能。 想學 go 又不想放棄 php 可以用玩下 CGO。 zephir 雖然運算效能不太好,但寫 PHP 擴充實作起來比較簡單。 推薦學習:《PHP影片教學》
以上是面試官:列舉幾種PHP拓展的實現手段及其表現比較?的詳細內容。更多資訊請關注PHP中文網其他相關文章!