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";
我知道这个脚本编写的并不是很好,尤其是中间那个讨厌的数组。我知道我可以生成一个随机数组。我也知道我可以从文件中读取这个数组。但是,我只想要一个不需要考虑其他因素的非常慢的冒泡算法。我不希望这个基准测试中出现任何额外的干扰。
docker container run --rm -v $(pwd):/script/ php:7.4 php /script/bubble.php
运行100次得到的平均时间为0.10253500938416秒——还不错。
docker container run --rm -v $(pwd):/script/ martinpham/php8:fpm-extra-alpine php /script/bubble.php
同样运行100次的平均时间为0.098223924636841秒。老实说,这个提升不怎么明显。
docker container run --rm -v $(pwd):/script/ keinos/php8-jit php /script/bubble.php
平均运行时间为0.053637981414795秒!JIT确实很惊喜!对于像我这样非常关注执行时间的人来说,这简直就是一个福音。虽然单看数字你没什么感觉(仅改善了0.04458594322秒),但换算成百分比后,就能更好地看清优势了:45.39%。
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版本不是最终版本,因此可能最终的发行版会增添其他改进。
-
我在测试结果中保留了很多位小数,虽然看着很罗嗦,但我只是想尽可能做到精确。
-
在基准测试中,我使用keinos和martinpham的docker镜像。其实还有很多其他工具,你可以选择自己喜欢的尝试一下。
-
我运行测试的笔记本电脑的配置为:Linux Manjaro、3 GHz Intel Core i5、32 GB RAM。在其他配置下,具体的数字可能会有所不同,但是我相信改进结果都很相似。
来源:
https://levelup.gitconnected.com/how-fast-is-php-8-going-to-be-f7fdc111cd6
·END·
PHP开源社区 进阶·提升·涨薪
文章评论