1. 程式人生 > 實用技巧 >php-8.0.0 效能測試

php-8.0.0 效能測試

測試環境為 apple m1
測試的 php 版本為 8.0.0, 對比的版本為 7.2.34,golang 1.15.6 amd64 with rosetta 2
php 通過 homebrew 安裝,當前 homebrew 已經支援 apple m1。
golang 為官網下載的安裝包。

考慮到 jit 的特性,我實現了一段快排演算法來測試

function qsort(&$nums, $from, $to) {
if ($from >= $to)
return;
$mid = partition($nums, $from, $to);
qsort($nums, $from, $mid - 1);
qsort($nums, $mid + 1, $to);
}

function partition(&$nums, $from, $to) {
$pivot = $nums[$from];
$i = $from + 1;
$j = $to;
while (true) {
while ($i < $to && $nums[$i] < $pivot) {
$i++;
}

    while ($j > $from && $nums[$j] > $pivot) {
        $j--;
    }

    if ($i >= $j) {
        break;
    }

    $t = $nums[$i];
    $nums[$i] = $nums[$j];
    $nums[$j] = $t;

    $i++;
    $j--;
}

$t = $nums[$j];
$nums[$j] = $pivot;
$nums[$from] = $t;

return $j;

}

我用程式實現生成了幾個隨機亂序後的數字的文字檔案,num-10.txt 即表示檔案內包含 10 個亂序後的數字。
因為 apple m1 非常快,我需要 100 萬個數字。
執行 php-8.0.0:
$ php8 qsort.php num-100w.txt
JIT is disabled
time cost: 2.3733279705048

因為 jit 僅支援 x86 平臺,因此 JIT 無法在 apple m1 上開啟,但即使只打開 opcache,也會看到有效能提升:
$ php8 -d opcache.enable_cli -d opcache.jit_buffer_size=100m -d opcache.jit=1255 qsort.php num-100w.txt
JIT is disabled
time cost: 2.0988318920135

使用 php-7.2.34 執行:
$ php qsort.php num-100w.txt
JIT is disabled
time cost: 2.8660459518433

使用 php-7.2.34 with opcache
$ php -d opcache.enable_cli qsort.php num-100w.txt
JIT is disabled
time cost: 2.5065989494324

golang 的版本如下

func qsort(nums []int, lo int, hi int) {
if lo >= hi {
return;
}
mid := partition(nums, lo, hi);
qsort(nums, lo, mid - 1);
qsort(nums, mid + 1, hi);
}

func partition(nums []int, lo int, hi int) int {
i := lo + 1;
j := hi;
pivot := nums[lo];
for {
for (i < hi && nums[i] < pivot) {
i++;
}
for (j > lo && nums[j] > pivot) {
j--;
}
if (i >= j) {
break;
}
nums[i], nums[j] = nums[j], nums[i];
i++;
j--;
}
nums[lo], nums[j] = nums[j], nums[lo];
return j;
}

用時是令人髮指的少:
$ go run qsort.go num-100w.txt
time cost 0.063744

為了試驗 php-8.0 開啟 JIT 後的效果,我找來了一臺 x86 的雲伺服器試驗,這臺雲服比 apple m1 效能差很多。
關閉 JIT
JIT is disabled
time cost: 31.630923986435

開啟 JIT
JIT is enabled
time cost: 13.33647108078

x86 上開啟 JIT 能有顯著的提升 57.8% 的效能提升。