{"id":568,"date":"2020-06-03T14:02:00","date_gmt":"2020-06-03T14:02:00","guid":{"rendered":"https:\/\/www.smarthost.eu\/blog\/?p=568"},"modified":"2020-06-03T14:40:10","modified_gmt":"2020-06-03T14:40:10","slug":"is-php-7-4-faster-than-7-3","status":"publish","type":"post","link":"https:\/\/www.smarthost.au\/blog\/is-php-7-4-faster-than-7-3","title":{"rendered":"Is PHP 7.4 faster than 7.3?"},"content":{"rendered":"\n<p>The PHP language is constantly being developed by their creators and new versions are constantly being released. On November 28, 2019, a stable version of PHP 7.4 was released, which brings new features, improvements and performance improvements.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is new in PHP 7.4?<\/h2>\n\n\n\n<p>What changes can we observe in the new PHP version compared to the previous ones?<\/p>\n\n\n\n<p><strong>Preloading <\/strong>&#8211; by using PHP and OPcache, which is installed by default on our servers, you can get better performance. The application code is first compiled and then saved into memory. This allows you to use the data that has been placed in the memory space and avoid recompiling the code<\/p>\n\n\n\n<p><strong>Arrow Functions<\/strong> &#8211;   thanks to introduced in the latest PHP arrow functions version we can reduce the amount of code and increase code readability. <\/p>\n\n\n\n<p>Code for PHP 7.3 and older versions:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>array_map(function (User $user) { \n    return $user->id; \n}, $users)<\/code><\/pre>\n\n\n\n<p>The same code written with the new arrow functions used in PHP 7.4:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>array_map(fn (User $user) => $user->id, $users)<\/code><\/pre>\n\n\n\n<p><strong>Spread operator <\/strong>&#8211; PHP 7.4 also introduces the possibility of using the spread operator, so we can increase the performance of our application.<\/p>\n\n\n\n<p>An example of using the spread operator:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$server= ['litespeed', 'redis', 'mariadb', 'php'];\n$hosting= ['domains', 'administration', ...$server, 'support'];<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">PHP 7.4 performance<\/h2>\n\n\n\n<p>To check the performance of PHP 7.4 and the difference in loading time of websites based on this version and on the previous 7.3 we conducted tests. For this end, we used three different scripts. Each of them was performed ten times, and then we drew the average from the results, which we have included in the table below:<\/p>\n\n\n\n<figure class=\"wp-block-table aligncenter\"><table class=\"\"><tbody><tr><td><\/td><td><strong>bench.php<\/strong><\/td><td><strong>micro_bench.php<\/strong><\/td><td><strong>php_benchmark.php<\/strong><\/td><td><strong>Total time<\/strong><\/td><\/tr><tr><td><strong>PHP 7.4<\/strong><\/td><td>0.322 s<\/td><td>1.925 s<\/td><td>0.727 s<\/td><td>2.974 s<\/td><\/tr><tr><td><strong>PHP 7.3<\/strong><\/td><td>0.255 s<\/td><td>1.696 s<\/td><td>0.661 s<\/td><td>2.612 s<\/td><\/tr><tr><td><strong>Time difference (the less the better)<\/strong><\/td><td><strong>0.067 s<\/strong><\/td><td><strong>0.229 s<\/strong><\/td><td><strong>0.066 s<\/strong><\/td><td><strong>0.362 s<\/strong><\/td><\/tr><tr><td><strong>Percentage acceleration of PHP 7.4 compared to PHP 7.3<\/strong><\/td><td><strong>26%<\/strong><\/td><td><strong>13%<\/strong><\/td><td><strong>9%<\/strong><\/td><td><strong>14%<\/strong><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>The difference between the individual tests is clearly seen in the following charts: <\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"817\" height=\"493\" src=\"https:\/\/www.smarthost.eu\/blog\/wp-content\/uploads\/2020\/06\/php73-vs-php74-compare-1.png\" alt=\"\" class=\"wp-image-576\" srcset=\"https:\/\/www.smarthost.au\/blog\/wp-content\/uploads\/2020\/06\/php73-vs-php74-compare-1.png 817w, https:\/\/www.smarthost.au\/blog\/wp-content\/uploads\/2020\/06\/php73-vs-php74-compare-1-300x181.png 300w, https:\/\/www.smarthost.au\/blog\/wp-content\/uploads\/2020\/06\/php73-vs-php74-compare-1-768x463.png 768w\" sizes=\"auto, (max-width: 817px) 100vw, 817px\" \/><\/figure><\/div>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"584\" height=\"354\" src=\"https:\/\/www.smarthost.eu\/blog\/wp-content\/uploads\/2020\/06\/php73-vs-php74-compare-2.png\" alt=\"\" class=\"wp-image-578\" srcset=\"https:\/\/www.smarthost.au\/blog\/wp-content\/uploads\/2020\/06\/php73-vs-php74-compare-2.png 584w, https:\/\/www.smarthost.au\/blog\/wp-content\/uploads\/2020\/06\/php73-vs-php74-compare-2-300x182.png 300w\" sizes=\"auto, (max-width: 584px) 100vw, 584px\" \/><\/figure><\/div>\n\n\n\n<p>Again, you can see that the latest version of PHP 7.4 is faster than the previous one. What is the situation with version 7.3, and even older (mostly unsupported) PHP versions? A year ago, we published the results on our blog at: <a href=\"https:\/\/www.smarthost.eu\/blog\/34\">We implement PHP 7.3 and compare it with other versions<\/a><\/p>\n\n\n\n<p>A few words about the scripts used:<\/p>\n\n\n\n<p><strong>bench.php<\/strong><\/p>\n\n\n\n<p>This script mainly performs tasks in loops, in addition to simple functions (which in the loop perform variable value increment, calculate the length of a string or call an empty function) also performs, among others the following tasks:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>calculates the Mandelbrot set,<\/li><li>Ackermann function,<\/li><li>calculates fibonacci sequence,<\/li><li>sorts the pile,<\/li><li>creates a multidimensional array.<\/li><\/ul>\n\n\n\n<p>Source:  <a href=\"https:\/\/github.com\/php\/php-src\/blob\/master\/Zend\/bench.php\">https:\/\/github.com\/php\/php-src\/blob\/master\/Zend\/bench.php<\/a> <\/p>\n\n\n\n<p><strong>mcro_bench.php<\/strong><\/p>\n\n\n\n<p>This script is also based on loops and executes, among others the following tasks:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>calls functions in the loop,<\/li><li>reads the value of the object&#8217;s property,<\/li><li>saves the value in the object&#8217;s property,<\/li><li>checks whether a value is assigned to the object property,<\/li><li>checks whether the object&#8217;s property value is not empty,<\/li><li>calls the object method,<\/li><li>reads the value of the static property of the object,<\/li><li>saves the value in the object&#8217;s static property,<\/li><li>check whether a value is assigned to the static property of the object,<\/li><li>checks whether the value of the object&#8217;s static property is not empty,<\/li><li>calls the object&#8217;s static method.<\/li><\/ul>\n\n\n\n<p>Source:  <a href=\"https:\/\/github.com\/php\/php-src\/blob\/master\/Zend\/micro_bench.php\">https:\/\/github.com\/php\/php-src\/blob\/master\/Zend\/micro_bench.php<\/a> <\/p>\n\n\n\n<p><strong>php_benchmark.php<\/strong><\/p>\n\n\n\n<p>The main task of this script is to perform standard operations on PHP and optionally on the database used by WordPress, Joomla, PrestaShop or other CMS:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>calculation of mathematical operations,<\/li><li>string operations,<\/li><li>calling a loop,<\/li><li>operations on<em> if else<\/em> conditional statements.<\/li><\/ul>\n\n\n\n<p>Source:  <a href=\"https:\/\/github.com\/vanilla-php\/benchmark-php\/blob\/master\/benchmark.php\">https:\/\/github.com\/vanilla-php\/benchmark-php\/blob\/master\/benchmark.php<\/a> <\/p>\n\n\n\n<p><strong>We encourage you to test your performance yourself and check for new PHP 7.4 features in Litespeed hosting. Each of our packages has the latest version set by default in PHP 7.4<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The PHP language is constantly being developed by their creators and new versions are constantly being released. On November 28, 2019, a stable version of PHP 7.4 was released, which brings new features, improvements and<a class=\"read-more\" href=\"https:\/\/www.smarthost.au\/blog\/is-php-7-4-faster-than-7-3\">Continue reading<\/a><\/p>\n","protected":false},"author":16,"featured_media":591,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,4],"tags":[76,185,188,187,186],"class_list":["post-568","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php","category-website-speed","tag-php","tag-php-7-4","tag-php-7-4-and-7-3-benchmark","tag-php-7-4-and-7-3-compare","tag-php-7-4-benchmark"],"_links":{"self":[{"href":"https:\/\/www.smarthost.au\/blog\/wp-json\/wp\/v2\/posts\/568","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.smarthost.au\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.smarthost.au\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.smarthost.au\/blog\/wp-json\/wp\/v2\/users\/16"}],"replies":[{"embeddable":true,"href":"https:\/\/www.smarthost.au\/blog\/wp-json\/wp\/v2\/comments?post=568"}],"version-history":[{"count":14,"href":"https:\/\/www.smarthost.au\/blog\/wp-json\/wp\/v2\/posts\/568\/revisions"}],"predecessor-version":[{"id":592,"href":"https:\/\/www.smarthost.au\/blog\/wp-json\/wp\/v2\/posts\/568\/revisions\/592"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.smarthost.au\/blog\/wp-json\/wp\/v2\/media\/591"}],"wp:attachment":[{"href":"https:\/\/www.smarthost.au\/blog\/wp-json\/wp\/v2\/media?parent=568"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.smarthost.au\/blog\/wp-json\/wp\/v2\/categories?post=568"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.smarthost.au\/blog\/wp-json\/wp\/v2\/tags?post=568"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}