开发者

Is type hinting helping the performance of PHP scripts?

Type hinting helps the compiler to assume the type of the variable, but, as the PHP is a dynamic scripting interpreted language, the question came to my mind if it's possible t开发者_运维技巧hat type hinting even makes the runtime faster or not?


PHP is a dynamic language.
Dynamic languages only support runtime checking.
Type hinting is runtime type checking.
Runtime type checking is bad for performance.
Therefore, type hinting is bad for performance.


Type hinting only hinders performance because it requires (for objects) to test the inheritance hierarchy. To make things worse, this kind of check is expensive in PHP because it is done in time proportional to the depth of the hierarchy.

A test for type, such the one done for the array hint is a much smaller overhead, though.

Furthermore, it is currently not used for any optimization purposes.

It is however, a useful defensive programming feature.


I did a benchmark

https://github.com/EFTEC/php-benchmarks#type-hinting (you can check the whole code, test it and check it by yourself).

/**
 * @param DummyClass $arg1
 * @param DummyClass $arg2
 *
 * @return DummyClass
 */
function php5($arg1,$arg2){
    return new DummyClass();
}
function php7(DummyClass $arg1,DummyClass $arg2): DummyClass {
    return new DummyClass();
}
  • php5 0.0006339550018310547 seconds
  • php7 0.0007991790771484375 seconds

And type hinting is around 10% slower than without type hinting. It is not considerably slow unless it is required in a vital part of the code (a huge loop for example) or we need raw performance.

I prefer PHPdoc, it works similar, it's more complete and it doesn't affect the performance.


The accepted answer and the other answers as well as the original question are wrong on an important aspect / technicality:

/**
 * @param string|null $input          <--- this is a type hint
 */
function test($input): string         <--- this is a type declaration
{
    return $input;
}
  • type hinting is an IDE feature (built on top of language functionality such as types, typed declarations, meta constructs, xDoc etc)
    • it is intended for development-time, typically for documentation and (before attributes) for metaprogramming
    • as such, the performance hit at run-time is probably negligible (if all you have is a bunch of PHPDocs) and the IDE, depending on implementation, might actually get faster (e.g. hinting small specific types instead of god objects as is typical in a javascript environment)
    • unless you're doing some sort of meta programming (doing stuff with PHPDoc), (bad) type hinting should not cause any compile-time or run-time problems
  • type declarations are a language feature
    • it is intended for the application run-time
    • bad/wrong type declarations will cause compile-time or run-time errors
    • some type declarations will (negligibly) slow down the application:
      function test(): SomeClass {
          return $x;  // $x can only be checked at run-time
      }
      
    • some type declarations are actually checked at compile-time and incur no additional performance hits (although in this case, the previous point still applies):
      interface IParent {
          public function test(): SomeClass;
      }
      interface IChild extends IParent {
          public function test(): SomeUnrelatedClass; // 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜