<返回更多

PHP 8 能有多快?

2020-02-23    
加入收藏

php-8将于今年年底发布,其最令人期待的功能之一就是JIT编译。让我们通过本文来看看这项功能对PHP脚本的速度有怎样的影响?

PHP 8 能有多快?

作者 | Pedro Escudero

译者 | 弯月,责编 | 郭芮

以下为译文:

今年年底,PHP-8即将发布。虽然这个主版本有很多新功能,例如联合类型、静态返回和弱映射等,但最令人期待的改进还是JIT编译器。

从理论上讲,JIT处理PHP脚本编译的方式能够提高应用程序的速度。但让我们来看看这种方法能否达到我们的期望。

请注意,在本文中使用的PHP-8不是最终版本。

首先,我们来编写一段运行的代码。我选择了冒泡排序,因为这是最差的排序算法,只有这种测试的时候我才能用这个排序算法。

<?php
function bubble_sort($array)
{
$start = microtime(true);
do
{
$sw = false;
for($i = 0, $size = count($array) - 1; $i < $size; $i++)
{
if( $array[$i] > $array[$i + 1] )
{
list( $array[$i + 1], $array[$i] ) =
array($array[$i], $array[$i + 1]);
$sw = true;
}
}
}
while($sw);
$end = microtime(true);
return $end - $start;
}
$array = array(1000, -202, 3, 0, 2, 77, 5, -1, 4, 34, -203, 1, 0.5 , -3, 0.88,
4000, 444, 0.99, -44, 4, 454443, -44444, 23, -201,
5000, 444, 0.99, -44, 4, 4553, -4, 253, -201,
6000, 444, 0.99, -44, 4, 443, -4, 2.3, -2.01,
39000, 444, 0.99, -44, 4, 53, -4, 23, -201,
309900, 444, 0.99, 23, 123, 453, -4, 23, -201,
309900, 444, 0.99, -4.14, 4, 453, -4, 23, -201,
309900, 444, 0.99, -4.44, 4, 453, -4, 23, -201,
30500, 1144, 0.99, -4.4, 4, 453, -4, 23, -201,
-3000, -444, 0.99, -44, 4, 453, -4, 23, -1,
3000, 424, 0.19, -44, 4, 453, -4, 23, -21,
300, 454, 0.99222, -44, 4, 453, -4, 23, -20.1,
301110, 4442, 0.991, -44, 4, 453, -4, 2.443, -201,
3033300, 33444, 0.49, -44, 4, 453, -4, 2.4443, -201,
4000, 444, 0.99, -44, 4, 454443, -44444, 23, -201,
5000, 444, 0.99, -44, 4, 4553, -4, 253, -201,
6000, 444, 0.99, -44, 4, 443, -4, 2.3, -2.01,
39000, 444, 0.99, -44, 4, 53, -4, 23, -201,
309900, 444, 0.99, 23, 123, 453, -4, 23, -201,
309900, 444, 0.99, -4.14, 4, 453, -4, 23, -201,
309900, 444, 0.99, -4.44, 4, 453, -4, 23, -201,
30500, 1144, 0.99, -4.4, 4, 453, -4, 23, -201,
-3000, -444, 0.99, -44, 4, 453, -4, 23, -1,
3000, 424, 0.19, -44, 4, 453, -4, 23, -21,
300, 454, 0.99222, -44, 4, 453, -4, 23, -20.1,
301110, 4442, 0.991, -44, 4, 453, -4, 2.443, -201,
3033300, 33444, 0.49, -44, 4, 453, -4, 2.4443, -201,
4000, 444, 0.99, -44, 4, 454443, -44444, 23, -201,
5000, 444, 0.99, -44, 4, 4553, -4, 253, -201,
6000, 444, 0.99, -44, 4, 443, -4, 2.3, -2.01,
39000, 444, 0.99, -44, 4, 53, -4, 23, -201,
309900, 444, 0.99, 23, 123, 453, -4, 23, -201,
309900, 444, 0.99, -4.14, 4, 453, -4, 23, -201,
309900, 444, 0.99, -4.44, 4, 453, -4, 23, -201,
30500, 1144, 0.99, -4.4, 4, 453, -4, 23, -201,
-3000, -444, 0.99, -44, 4, 453, -4, 23, -1,
3000, 424, 0.19, -44, 4, 453, -4, 23, -21,
300, 454, 0.99222, -44, 4, 453, -4, 23, -20.1,
301110, 4442, 0.991, -44, 4, 453, -4, 2.443, -201,
3033300, 33444, 0.49, -44, 4, 453, -4, 2.4443, -201,
4000, 444, 0.99, -44, 4, 454443, -44444, 23, -201,
5000, 444, 0.99, -44, 4, 4553, -4, 253, -201,
6000, 444, 0.99, -44, 4, 443, -4, 2.3, -2.01,
39000, 444, 0.99, -44, 4, 53, -4, 23, -201,
309900, 444, 0.99, 23, 123, 453, -4, 23, -201,
309900, 444, 0.99, -4.14, 4, 453, -4, 23, -201,
309900, 444, 0.99, -4.44, 4, 453, -4, 23, -201,
30500, 1144, 0.99, -4.4, 4, 453, -4, 23, -201,
-3000, -444, 0.99, -44, 4, 453, -4, 23, -1,
3000, 424, 0.19, -44, 4, 453, -4, 23, -21,
300, 454, 0.99222, -44, 4, 453, -4, 23, -20.1,
301110, 4442, 0.991, -44, 4, 453, -4, 2.443, -201,
3033300, 33444, 0.49, -44, 4, 453, -4, 2.4443, -201,
4000, 444, 0.99, -44, 4, 454443, -44444, 23, -201,
5000, 444, 0.99, -44, 4, 4553, -4, 253, -201,
6000, 444, 0.99, -44, 4, 443, -4, 2.3, -2.01,
39000, 444, 0.99, -44, 4, 53, -4, 23, -201,
309900, 444, 0.99, 23, 123, 453, -4, 23, -201,
309900, 444, 0.99, -4.14, 4, 453, -4, 23, -201,
309900, 444, 0.99, -4.44, 4, 453, -4, 23, -201,
30500, 1144, 0.99, -4.4, 4, 453, -4, 23, -201,
-3000, -444, 0.99, -44, 4, 453, -4, 23, -1,
3000, 424, 0.19, -44, 4, 453, -4, 23, -21,
300, 454, 0.99222, -44, 4, 453, -4, 23, -20.1,
301110, 4442, 0.991, -44, 4, 453, -4, 2.443, -201,
3033300, 33444, 0.49, -44, 4, 453, -4, 2.4443, -201,
4000, 444, 0.99, -44, 4, 454443, -44444, 23, -201,
5000, 444, 0.99, -44, 4, 4553, -4, 253, -201,
6000, 444, 0.99, -44, 4, 443, -4, 2.3, -2.01,
39000, 444, 0.99, -44, 4, 53, -4, 23, -201,
309900, 444, 0.99, 23, 123, 453, -4, 23, -201,
309900, 444, 0.99, -4.14, 4, 453, -4, 23, -201,
309900, 444, 0.99, -4.44, 4, 453, -4, 23, -201,
30500, 1144, 0.99, -4.4, 4, 453, -4, 23, -201,
-3000, -444, 0.99, -44, 4, 453, -4, 23, -1,
3000, 424, 0.19, -44, 4, 453, -4, 23, -21,
300, 454, 0.99222, -44, 4, 453, -4, 23, -20.1,
301110, 4442, 0.991, -44, 4, 453, -4, 2.443, -201,
3033300, 33444, 0.49, -44, 4, 453, -4, 2.4443, -201,
4000, 444, 0.99, -44, 4, 454443, -44444, 23, -201,
5000, 444, 0.99, -44, 4, 4553, -4, 253, -201,
6000, 444, 0.99, -44, 4, 443, -4, 2.3, -2.01,
39000, 444, 0.99, -44, 4, 53, -4, 23, -201,
309900, 444, 0.99, 23, 123, 453, -4, 23, -201,
309900, 444, 0.99, -4.14, 4, 453, -4, 23, -201,
309900, 444, 0.99, -4.44, 4, 453, -4, 23, -201,
30500, 1144, 0.99, -4.4, 4, 453, -4, 23, -201,
-3000, -444, 0.99, -44, 4, 453, -4, 23, -1,
3000, 424, 0.19, -44, 4, 453, -4, 23, -21,
300, 454, 0.99222, -44, 4, 453, -4, 23, -20.1,
301110, 4442, 0.991, -44, 4, 453, -4, 2.443, -201,
3033300, 33444, 0.49, -44, 4, 453, -4, 2.4443, -201,
4000, 444, 0.99, -44, 4, 454443, -44444, 23, -201,
5000, 444, 0.99, -44, 4, 4553, -4, 253, -201,
6000, 444, 0.99, -44, 4, 443, -4, 2.3, -2.01,
39000, 444, 0.99, -44, 4, 53, -4, 23, -201,
309900, 444, 0.99, 23, 123, 453, -4, 23, -201,
309900, 444, 0.99, -4.14, 4, 453, -4, 23, -201,
309900, 444, 0.99, -4.44, 4, 453, -4, 23, -201,
30500, 1144, 0.99, -4.4, 4, 453, -4, 23, -201,
-3000, -444, 0.99, -44, 4, 453, -4, 23, -1,
3000, 424, 0.19, -44, 4, 453, -4, 23, -21,
300, 454, 0.99222, -44, 4, 453, -4, 23, -20.1,
301110, 4442, 0.991, -44, 4, 453, -4, 2.443, -201,
3033300, 33444, 0.49, -44, 4, 453, -4, 2.4443, -201,
4000, 444, 0.99, -44, 4, 454443, -44444, 23, -201,
5000, 444, 0.99, -44, 4, 4553, -4, 253, -201,
6000, 444, 0.99, -44, 4, 443, -4, 2.3, -2.01,
39000, 444, 0.99, -44, 4, 53, -4, 23, -201,
309900, 444, 0.99, 23, 123, 453, -4, 23, -201,
309900, 444, 0.99, -4.14, 4, 453, -4, 23, -201,
309900, 444, 0.99, -4.44, 4, 453, -4, 23, -201,
30500, 1144, 0.99, -4.4, 4, 453, -4, 23, -201,
-3000, -444, 0.99, -44, 4, 453, -4, 23, -1,
3000, 424, 0.19, -44, 4, 453, -4, 23, -21,
300, 454, 0.99222, -44, 4, 453, -4, 23, -20.1,
301110, 4442, 0.991, -44, 4, 453, -4, 2.443, -201,
3033300, 33444, 0.49, -44, 4, 453, -4, 2.4443, -201,
4000, 444, 0.99, -44, 4, 454443, -44444, 23, -201,
5000, 444, 0.99, -44, 4, 4553, -4, 253, -201,
6000, 444, 0.99, -44, 4, 443, -4, 2.3, -2.01,
39000, 444, 0.99, -44, 4, 53, -4, 23, -201,
309900, 444, 0.99, 23, 123, 453, -4, 23, -201,
309900, 444, 0.99, -4.14, 4, 453, -4, 23, -201,
309900, 444, 0.99, -4.44, 4, 453, -4, 23, -201,
30500, 1144, 0.99, -4.4, 4, 453, -4, 23, -201,
-3000, -444, 0.99, -44, 4, 453, -4, 23, -1,
3000, 424, 0.19, -44, 4, 453, -4, 23, -21,
300, 454, 0.99222, -44, 4, 453, -4, 23, -20.1,
301110, 4442, 0.991, -44, 4, 453, -4, 2.443, -201,
3033300, 33444, 0.49, -44, 4, 453, -4, 2.4443, -201,
4000, 444, 0.99, -44, 4, 454443, -44444, 23, -201,
5000, 444, 0.99, -44, 4, 4553, -4, 253, -201,
6000, 444, 0.99, -44, 4, 443, -4, 2.3, -2.01,
39000, 444, 0.99, -44, 4, 53, -4, 23, -201,
309900, 444, 0.99, 23, 123, 453, -4, 23, -201,
309900, 444, 0.99, -4.14, 4, 453, -4, 23, -201,
309900, 444, 0.99, -4.44, 4, 453, -4, 23, -201,
30500, 1144, 0.99, -4.4, 4, 453, -4, 23, -201,
-3000, -444, 0.99, -44, 4, 453, -4, 23, -1,
3000, 424, 0.19, -44, 4, 453, -4, 23, -21,
300, 454, 0.99222, -44, 4, 453, -4, 23, -20.1,
301110, 4442, 0.991, -44, 4, 453, -4, 2.443, -201,
3033300, 33444, 0.49, -44, 4, 453, -4, 2.4443, -201,
4000, 444, 0.99, -44, 4, 454443, -44444, 23, -201,
5000, 444, 0.99, -44, 4, 4553, -4, 253, -201,
6000, 444, 0.99, -44, 4, 443, -4, 2.3, -2.01,
39000, 444, 0.99, -44, 4, 53, -4, 23, -201,
309900, 444, 0.99, 23, 123, 453, -4, 23, -201,
309900, 444, 0.99, -4.14, 4, 453, -4, 23, -201,
309900, 444, 0.99, -4.44, 4, 453, -4, 23, -201,
30500, 1144, 0.99, -4.4, 4, 453, -4, 23, -201,
-3000, -444, 0.99, -44, 4, 453, -4, 23, -1,
3000, 424, 0.19, -44, 4, 453, -4, 23, -21,
300, 454, 0.99222, -44, 4, 453, -4, 23, -20.1,
301110, 4442, 0.991, -44, 4, 453, -4, 2.443, -201,
3033300, 33444, 0.49, -44, 4, 453, -4, 2.4443, -2010000
);
echo "n";
echo bubble_sort($array);
echo "n";
?>

我知道这个脚本编写的并不是很好,尤其是中间那个讨厌的数组。我知道我可以生成一个随机数组。我也知道我可以从文件中读取这个数组。但是,我只想要一个不需要考虑其他因素的非常慢的冒泡算法。我不希望这个基准测试中出现任何额外的干扰。

首先,我们在当前的PHP 7.4版本中运行脚本:

Docker container run --rm -v $(pwd):/script/ php:7.4 php /script/bubble.php

运行100次得到的平均时间为0.10253500938416秒——还不错。

现在我们在PHP-8中运行,先不要激活JIT。

docker container run --rm -v $(pwd):/script/ martinpham/php8:fpm-extra-alpine php /script/bubble.php

同样运行100次的平均时间为0.098223924636841秒。老实说,这个提升不怎么明显。

下面,我们来测试期待已久的JIT:

docker container run --rm -v $(pwd):/script/ keinos/php8-jit php /script/bubble.php

平均运行时间为0.053637981414795秒!JIT确实很惊喜!对于像我这样非常关注执行时间的人来说,这简直就是一个福音。虽然单看数字你没什么感觉(仅改善了0.04458594322秒),但换算成百分比后,就能更好地看清优势了:45.39%。

我想从更多的角度来对比,因此我使用了2009年的PHP-5.3版本来运行这段代码。

docker container run --rm -v $(pwd):/script/ php:5.3 php /script/bubble.php

平均花费了0.64574003219604秒。比激活了JIT的PHP-8满了0.5921020508(下降了-1,103.88)!我的天,2009年的时候我们是怎么过来的?

PHP 8 能有多快?

注意:

原文:https://levelup.gitconnected.com/how-fast-is-php-8-going-to-be-f7fdc111cd6

作者:Pedro Escudero,工程副总裁@zinio.com。

本文为 CSDN 翻译,转载请注明来源出处。

声明:本站部分内容来自互联网,如有版权侵犯或其他问题请与我们联系,我们将立即删除或处理。
▍相关推荐
更多资讯 >>>