当前位置: 代码迷 >> 综合 >> PHP8 JIT性能测试
  详细解决方案

PHP8 JIT性能测试

热度:71   发布时间:2023-12-09 10:32:28.0
测试代码 testjit.php
<?phpfunction feb($n): int
{
    if($n == 1){
    return 1;}if($n == 2){
    return 2;}return feb($n-1) + feb($n-2);
}$n = 40;$start = microtime(true);echo feb($n), PHP_EOL;$end = microtime(true);echo $end-$start, PHP_EOL;
docker安装PHP8.0
$ docker pull php:8.0-cli
$ docker run -itd --name php8.0-cli -v /data/www:/www php:8.0-cli
$ docker exec -it php8.0-cli bash$ cd /www 
$ php testjit.php165580141
25.239120006561php7.2 是 28 秒。
配置php.ini
$ php -i |grep php.iniConfiguration File (php.ini) Path => /usr/local/etc/php没有列出php.ini文件那说明使用的是默认值$ cd /usr/local/etc/php
$ ls conf.d  php.ini-development  php.ini-production$ cp php.ini-production php.ini$ php -i | grep php.iniConfiguration File (php.ini) Path => /usr/local/etc/phpLoaded Configuration File => /usr/local/etc/php/php.ini这次有了$ php -m 发现没有开启 Zend OPcache $ echo zend_extension=opcache >> php.ini
带参数执行命令行
$ cd /www
$ php -d opcache.enable_cli=1 -d opcache.jit=1205 -d opcache.jit_buffer_size=64M testjit.php1655801416.0113999843597

效果还是很明显的吧,4倍左右。

或者编辑配置文件,开启jit

$ apt-get update
$ apt-get install -y vim $ vi /usr/local/etc/php/php.iniopcache.enable_cli=1opcache.jit=1205opcache.jit_buffer_size=64M$ php -i |grep jitauto_globals_jit => On => Onpcre.jit => 1 => 1opcache.jit => 1205 => 1205opcache.jit_bisect_limit => 0 => 0opcache.jit_blacklist_root_trace => 16 => 16opcache.jit_blacklist_side_trace => 8 => 8opcache.jit_buffer_size => 64M => 64Mopcache.jit_debug => 0 => 0opcache.jit_hot_func => 127 => 127opcache.jit_hot_loop => 64 => 64opcache.jit_hot_return => 8 => 8opcache.jit_hot_side_exit => 8 => 8opcache.jit_max_exit_counters => 8192 => 8192opcache.jit_max_loop_unrolls => 8 => 8opcache.jit_max_polymorphic_calls => 2 => 2opcache.jit_max_recursive_calls => 2 => 2opcache.jit_max_recursive_returns => 2 => 2opcache.jit_max_root_traces => 1024 => 1024opcache.jit_max_side_traces => 128 => 128opcache.jit_prof_threshold => 0.005 => 0.005$ php testjit.php1655801415.8304328918457
对比Golang
package mainimport ("fmt""time"
)func main() {
    n := 40start := time.Now()fmt.Println(feb(n))end := time.Now()fmt.Println(end.Sub(start))
}func feb(n int) int {
    if n == 1 {
    return 1}if n == 2 {
    return 2}return feb(n-1) + feb(n-2)
}
docker 安装golang
$ docker pull golang:1.14.13
$ docker run -itd --name go1.14 -v /data/www:/www golang:1.14.13
$ docker exec -it go1.14 bash
$ go version
$ go env配置环境变量$ echo export GOPATH=/data/www/golang/path >> /etc/profile
$ echo export GOPROXY=https://goproxy.io >> /etc/profile
$ echo export GO111MODULE=on >> /etc/profile
$ echo export PATH=$PATH:/usr/local/go/bin >> /etc/profile
$ echo export PATH=$PATH:/data/www/golang/path/bin >> /etc/profile
$ source /etc/profile
$ go env
$ cd /www/golang/project/demo1/
$ go build main48.go
$ ./main481655801412.489641839s$ go run main48.go1655801412.565413202s$ time go run main48.go1655801412.481155007sreal    0m3.328suser    0m2.866ssys     0m0.390s

但是相对于强类型编译型语言还是慢一些。

关于 jit 配置:https://www.laruence.com/2020/06/27/5963.html#comment-254634