官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 南开区| 北海市| 策勒县| 维西| 巫溪县| 腾冲县| 军事| 通海县| 宜昌市| 石城县| 泾川县| 舞钢市| 甘肃省| 古田县| 陆丰市| 图们市| 潼关县| 盐源县| 扎鲁特旗| 仁寿县| 垣曲县| 宜章县| 恩施市| 颍上县| 莒南县| 新疆| 和田县| 宿迁市| 济宁市| 曲麻莱县| 开远市| 新建县| 遂溪县| 呼伦贝尔市| 灵武市| 伽师县| 客服| 黑龙江省| 和平县| 广灵县| 皋兰县|