PHP-8 akan dikeluarkan pada hujung tahun ini dan ianya merupakan satu-satunya perkara yang akan mengujakan kami dengan fiturnya yang baru iaitu JIT compilation. Jom kita lihat bagaimana ianya dapat menambahbaik kelajuan skrip PHP.

Lights in the night
Photo by Marc Sendra Martorell / Unsplash

Hujung tahun ini, kita akan dapat menyaksikan kehebatan PHP-8. Antara yang kita dapat lihat adalah seperti union types, static return, weak maps, dan yang paling seronok adalah penambah-baikan JIT Compiler.

Secara teori, JIT akan menambahkan kelajuan aplikasi atas sebab caranya mengendalikan skrip PHP. Tetapi, mari kita lihat jika ianya dapat memenuhi kehendak kita sebagai pengatur-cara PHP.

Nota: Versi PHP-8 yang saya akan gunakan masih belum final

Pertama sekali, saya akan gunakan bubble sort sebagai contoh sorting.

<?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";
?>

Saya tahu kod ini tidak baik. Boleh sahaja saya generate random array tetapi apa yang saya cuba tunjukkan hanyalah contoh untuk bubble sorting.

Jika saya run kod di atas dengan menggunakan PHP 7.4...

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

Saya akan run kod di atas 100 kali dan secara purata, ianya selesai dalam tempoh 0.11 saat. Yep, amat baik sekali.

Okey, sekarang kita cuba pula dengan menggunakan PHP-8 tanpa JIT dan purata yang saya dapat adalah 0.10 saat.

Yes. Ada penambah-baikkan tetapi masih lagi kecil.

Sekarang pula kita cuba enablekan JIT dan hasilnya amat menakjubkan sekali. Masa puratanya adalahh 0.05 saat. Hampir separuh lebih laju.

Jadi, saya rasa PHP tidak akan ketinggalan zaman atas sebab keluarnya kod-kod malenia seperti NodeJS. Teruskan menggunakan PHP dan ianya tidak akan mengecewakan anda.