【问题标题】:What is the performance overhead of type-hinting in PHP?PHP 中类型提示的性能开销是多少?
【发布时间】:2014-04-29 17:29:46
【问题描述】:

PHP 中类型提示的性能开销有多大 - 在决定是否使用它时是否需要考虑?

【问题讨论】:

  • 没有。它不是。严重地。你会浪费更多的时间来修复一个没有被捕获的错误,因为你没有使用类型提示,而不是类型提示将在运行时产生。如果您真的想知道,请对其进行基准测试。
  • 类型提示只会让你的代码更有条理。不要犹豫使用它。不要浪费时间对其进行基准测试。
  • 平均而言几乎没有区别:3v4l.org/lWgY6/perf#tabs, 3v4l.org/110SL/perf#tabs
  • @deceze 有趣的是,它看起来像 HHVM 10% 的改进。我猜它实际上使用 typehints 来优化?
  • @Sz.:“不重要,但也不是完全可以忽略不计”-您在这里回答了问题。问题是开销是否“显着”,而不是“完全可以忽略不计”。你的cmets没有帮助!在这种情况下,指责人们教条主义并不好,而且完全没有必要。

标签: php performance type-hinting


【解决方案1】:

不,这并不重要。如果你需要做一些算法密集型的事情,比如声音处理或 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"));

在我的电脑上,性能是一样的。有时它更快,有时没有打字提示:

$ php Documents/type_hinting.php 
with typehint: 0.432s
dont typehint: 0.428s

$ php Documents/type_hinting.php 
with typehint: 0.414s
dont typehint: 0.416s

【讨论】:

  • 如果更现实的场景涉及单独文件中的类层次结构(甚至可能是命名空间/自动加载)呢?
  • 我的例子不切实际,因为它高估了类型提示的影响。在我的示例中,有 1) 类型提示,2) 函数调用 (with_typehint),3) 另一个函数调用 (count),4) 比较,5) “隐式”返回语句。所以我 20% 的代码受类型提示的影响。在一个现实的例子中,不到 1% 的时间花在类型提示发生的地方,因此进一步减少了差异。 tl;博士:如果你不能用我的例子来测量时间差,你也不会用任何现实的例子来测量时间差。
  • @Alastair 如果您不确定,请编写另一个测试。但恕我直言,这是浪费时间。
  • “如果你想要硬数据,就做一个基准测试......” 实际上,这是答案中唯一可以接受的部分,Mike。如果你的测试变得可靠,那也很好,但它如此不稳定的事实意味着它很遗憾毫无用处。然而,你继续得出结论是“相同的”......;)(注意:我绝不暗示这是一个显着的开销,很明显。我只是在与趋势作斗争其中教条主义被用来代替“硬数据”。)
  • @Sz.:教条主义?认真的?为什么会有敌意?
【解决方案2】:

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

$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;

我的计算机上 1 000 000 个循环的差异:

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 

在我将stdClass 替换为array 后,我计算机上1 000 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 

【讨论】:

    猜你喜欢
    • 2017-09-27
    • 2013-05-06
    • 2012-12-12
    • 1970-01-01
    • 1970-01-01
    • 2019-04-26
    • 2012-03-07
    • 2021-07-26
    相关资源
    最近更新 更多