0009-PHP8 新特性: JIT

PHP-教程   2023-04-18 16:15   534   0  

一、PHP 运行速度测试

1、新建php文件

<?php
// 返回当前时间戳的微秒数
$start = microtime(true) ;

$total = 0;
for ($i=0; $i < 1000000; $i++) {
 $total += $i;
}

echo "Count: ".$i.",Total: " . $total . "\n";

// 返回当前时间戳的微秒数
$end = microtime(true);

// 计算开始到结束,所用时间
$spend = floor(($end - $start) * 1000);

echo "Time use: " . $spend . " ms\n";
?>

2、测速

  • 此时没有开启Opcache速度差不多。

二、JIT (即时编译)编译器

  • JIT (Just-In-Time)即时编译器是 PHP 8.0 中最重要的新功能之一,可以极大地提高性能。

  • JIT 编译器将作为扩展集成到 phpOpcache 扩展 用于运行时将某些操作码直接转换为从 cpu 指令。 仅在启用 opcache 的情况下,JIT 才有效

1、Opcache 扩展

  • OPcache 通过将 PHP 脚本预编译的字节码存储到共享内存中来提升 PHP 的性能, 存储预编译字节码的好处就是:省去了每次加载和解析 PHP 脚本的开销。

2、Opcache 开启

  • 文件位置:php.ini

zend_extension=opcache

3、Opcache 配置

; Determines if Zend OPCache is enabled
opcache.enable=1

; Determines if Zend OPCache is enabled for the CLI version of PHP
opcache.enable_cli=0

; The OPcache shared memory storage size.
opcache.memory_consumption=128

; The amount of memory for interned strings in Mbytes.
opcache.interned_strings_buffer=8

; The maximum number of keys (scripts) in the OPcache hash table.
; Only numbers between 200 and 1000000 are allowed.
opcache.max_accelerated_files=10000

4、JIT 配置(新增)

opcache.jit=tracing
opcache.jit_buffer_size=100M

5、JIT 速度测试

  • PHP8 速度快很多。

6、php 扩展目录

extension_dir = "ext"



博客评论
还没有人评论,赶紧抢个沙发~
发表评论
说明:请文明发言,共建和谐网络,您的个人信息不会被公开显示。