Strict typing

When a language is dynamically typed, that is to say, it has loosely typed variables, it provides a higher level of abstraction that boosts the developer's productivity, but doesn't offer the best performance since its compiler has more work to do when trying to determine the datatypes of its variables. It comes as no surprise that strongly typed languages have always had better performance at runtime than loosely typed ones. This conclusion was confirmed by Facebook's HipHop project, which conducted benchmark tests with different languages and came to the conclusion that statically compiled languages always execute more quickly and consume less memory than dynamic ones.

Although PHP 7 is still a loosely typed language, it now offers the possibility to strict type variables and function signatures. This can be easily tested by executing the following code example. Let's run the following code to see its current performance:

// chap3_strict_typing.php 
 
declare(strict_types = 0); 
 
$start = microtime(true); 
 
function test ($variable) 
{ 
    $variable++; 
 
    return "$variable is a test."; 
} 
 
ob_start(); 
 
for ($x = 0; $x < 1000000; $x++) { 
 
    $array[$x] = (string) $x; 
 
    echo test($array[$x]) . PHP_EOL; 
 
} 
 
$time = microtime(true) - $start; 
 
ob_clean(); 
 
ob_end_flush(); 
 
echo 'Time elapsed: ' . $time . PHP_EOL; 

Here are the results of running this script using Blackfire.io:

The profiling report when omitting to do strict typing of variables and function signatures

Now, let's replace the code with the following:

// chap3_strict_typing_modified.php 
 
declare(strict_types = 1); 
 
$start = microtime(true); 
 
function test (int $variable) : string 
{ 
    $variable++; 
 
    return $variable . ' is a test.'; 
} 
 
ob_start(); 
 
for ($x = 0; $x < 1000000; $x++) { 
 
    $array[$x] = (int) $x; 
 
    echo test($array[$x]) . PHP_EOL; 
 
} 
 
$time = microtime(true) - $start; 
 
ob_clean(); 
 
ob_end_flush(); 
 
echo 'Time elapsed: ' . $time . PHP_EOL; 

If we execute it, we will immediately see the difference in performance:

The profiling report when strict typing variables and function signatures

The performance boost can also be seen using the microtime() function. Let's run both versions of our script and see the result:

Comparing script performance with the microtime() function

In order to fully benefit from PHP's new AST and SSA features, developers should try to strictly type variables and function signatures as much as possible. This will become especially true when the Zend Engine gets, in future releases, a Just-In-Time (JIT) compiler as this will allow for further optimizations solely based on type inference.

Also, an added bonus of strict typing is that it lets the compiler manage an aspect of code quality by eliminating the necessity of having unit tests that simply make sure that functions are behaving as expected when receiving unexpected inputs.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset