Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP中类型暗示的性能开销是多少?_Php_Performance_Type Hinting - Fatal编程技术网

PHP中类型暗示的性能开销是多少?

PHP中类型暗示的性能开销是多少?,php,performance,type-hinting,Php,Performance,Type Hinting,PHP中类型暗示的性能开销有多大?它是否足够重要,可以作为决定是否使用它的一个考虑因素?不,它不重要。如果你需要做一些算法密集型的事情,比如声音处理或3D编程,你应该使用另一种编程语言 如果你想要硬数据,做一个基准 <?php function with_typehint(array $bla) { if(count($bla) > 55) { die("Array to big"); } } function dont_typehint($bla

PHP中类型暗示的性能开销有多大?它是否足够重要,可以作为决定是否使用它的一个考虑因素?

不,它不重要。如果你需要做一些算法密集型的事情,比如声音处理或3D编程,你应该使用另一种编程语言


如果你想要硬数据,做一个基准

<?php

function with_typehint(array $bla)
{
    if(count($bla) > 55) {
        die("Array to big");
    }
}

function dont_typehint($bla)
{
    if(count($bla) > 55) {
        die("Array to big");
    }   
}

function benchmark($fn)
{
    $start = microtime(TRUE);
    $array = array("bla", 3);
    for($i=0; $i<1000000; $i++) {
        $fn($array);
    }
    $end = microtime(TRUE);
    return $end-$start;
}

printf("with typehint: %.3fs\n", benchmark("with_typehint"));
printf("dont typehint: %.3fs\n", benchmark("dont_typehint"));

您可以通过创建一个简单的bechmark来找到答案,例如:

$test1 = function(stdClass $obj){};
$test2 = function($obj){};

$begin = microtime(true);
for ($i = 0; $i < 1000000; $i++){
    $test1(new stdClass);
}
$end = microtime(true);

$timeElapsedTest1 = $end - $begin;

$begin = microtime(true);
for ($i = 0; $i < 1000000; $i++){
    $test2(new stdClass);
}
$end = microtime(true);

$timeElapsedTest2 = $end - $begin;

echo 'difference: ', $timeElapsedTest1 - $timeElapsedTest2;
在我将
stdClass
替换为
array
之后,我的计算机上1000 000个循环的差异发生了一些变化:

1.  0.00209307670593 ms 
2.  0.00217390060425 ms 
3.  0.00558805465698 ms 
4.  0.00264406204224 ms 
5.  0.00367116928101 ms 
6.  0.00262594223022 ms 
7.  0.00353169441223 ms 
8.  0.00217604637146 ms 
9.  0.00049090385437 ms 
10. 0.002516746521 ms 

不,不是。认真地由于没有使用类型暗示,您将损失更多的时间来修复未被捕获的错误,而类型暗示在运行时将不会累积。如果你真的想知道的话,就对它进行基准测试。类型暗示只会让你的代码更有条理。不要犹豫使用它。不要浪费时间对其进行基准测试。平均而言,几乎没有什么区别:,@deceze有趣的是,它看起来在HHVM中提高了10%。我猜它实际上是使用类型提示来优化的?@Sz.:“不重要,但也不是完全可以忽略不计”——这里你回答了这个问题。问题是,开销是否“重大”,而不是“完全可以忽略不计”。你的评论没有帮助!在这种情况下,指责人们教条主义是不好的,也是完全没有必要的。那么一个更现实的场景是什么,涉及到独立文件中的类层次结构(甚至可能是命名空间/自动加载的)?我的示例是不现实的,因为它高估了类型暗示的影响。在我的示例中,有1)类型暗示,2)函数调用(带有_typehint),3)另一个函数调用(count),4)比较,5)一个“隐式”返回语句。因此,我的代码有20%受到类型暗示的影响。在一个现实的例子中,只有不到1%的时间会花在类型暗示发生的地方,因此会进一步减少差异。tl;dr:如果你不能用我的例子来测量时差,你也不会用任何实际的例子来测量时差。@Alastair如果你不确定,就写另一个测试。但我认为这是浪费时间。“如果你想要硬数据,就做一个基准……”事实上,这是答案中唯一可以接受的部分,迈克。如果你的测试是可靠的,那也会很好,但事实上它是如此不稳定,这意味着它不幸是无用的。然而,你继续得出结论,结果是“一样的”…)(注:显然,我并不是在暗示这是一项重大开销。我只是在与教条主义取代“硬数据”的趋势作斗争。)@Sz.:教条主义?真的?为什么会有敌意?
1.  0.0994789600372 ms 
2.  0.0944871902466 ms 
3.  0.103265047073 ms 
4.  0.0899112224579 ms 
5.  0.0860922336578 ms 
6.  0.0973558425903 ms 
7.  0.0905900001526 ms 
8.  0.0891189575195 ms 
9.  0.09983086586 ms 
10. 0.0914621353149 ms 
1.  0.00209307670593 ms 
2.  0.00217390060425 ms 
3.  0.00558805465698 ms 
4.  0.00264406204224 ms 
5.  0.00367116928101 ms 
6.  0.00262594223022 ms 
7.  0.00353169441223 ms 
8.  0.00217604637146 ms 
9.  0.00049090385437 ms 
10. 0.002516746521 ms