Performance Implications of Type Hinting

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Performance Implications of Type Hinting

Post by Ollie Saunders »

Does type hinting slow things down?
Short answer: yes!

Here's how it was done:

Code: Select all

class A {}
function typeHinted(A $arg) {}
function notTypeHinted($arg) {}

function microtime_float()
{
   list($usec, $sec) = explode(' ', microtime());
   return (float)($usec + $sec);
}

$tests = 7;
for ($i=0; $i<$tests; $i++) {
    $iterations = 250000;
    $a = new A();
    $time['start'] = microtime_float();
    for ($k=0; $k<$iterations; $k++) {
        typeHinted($a);
    }
    $time['end'] = microtime_float();
    $time['length'][] = $time['end'] - $time['start'];
}
print_r($time['length']);
Results for typeHinted()

Code: Select all

Array
(
    [0] => 0.356354951859
    [1] => 0.356025934219
    [2] => 0.352389812469
    [3] => 0.356775045395
    [4] => 0.360638856888
    [5] => 0.354598045349
    [6] => 0.356450080872
)
Results for notTypeHinted()

Code: Select all

Array
(
    [0] => 0.292051792145
    [1] => 0.286281108856
    [2] => 0.280771970749
    [3] => 0.285759210587
    [4] => 0.283509969711
    [5] => 0.286795139313
    [6] => 0.283569812775
)
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

Of course it does, but how does it compare to the following?

Code: Select all

class A {}

function hinted (A $var) {}
function NotHinted ($var) { if ($var instanceof A) return; }
(not got access to a parser atm)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

<?php

class A {}

function typeHinted(A $arg)
{
    return true;
}

function notTypeHinted($arg)
{
    return is_null($arg) || $arg instanceof A;
}

function microtime_float()
{
    list($usec, $sec) = explode(' ', microtime());
    return (float)($usec + $sec);
}

$tests = 7;
$iterations = 250000;
$time = array();
$a = new A();
for ($i=0; $i<$tests; $i++) {
    $time['start'] = microtime_float();
    for ($k=0; $k<$iterations; $k++) {
        typeHinted($a);
    }
    $time['end'] = microtime_float();
    $time['length'][] = $time['end'] - $time['start'];
}
print_r($time['length']); 

$time = array();
for ($i=0; $i<$tests; $i++) {
    $time['start'] = microtime_float();
    for ($k=0; $k<$iterations; $k++) {
        notTypeHinted($a);
    }
    $time['end'] = microtime_float();
    $time['length'][] = $time['end'] - $time['start'];
}
print_r($time['length']); 

?>

Code: Select all

Array
(
    [0] => 3.6643950939178
    [1] => 3.7446830272675
    [2] => 3.7304089069366
    [3] => 3.7364468574524
    [4] => 3.7444851398468
    [5] => 3.7310190200806
    [6] => 3.7463190555573
)
Array
(
    [0] => 4.5914969444275
    [1] => 4.6461958885193
    [2] => 4.6359140872955
    [3] => 4.6479008197784
    [4] => 4.6164281368256
    [5] => 4.6475148200989
    [6] => 4.6176421642303
)
:)
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

typeHinted is 24.74% slower than notTypeHinted
and instanceof is 24.16% slower than typeHinted.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

also add up the overhead of throwing an exception, or trigger_error() to match the full functionality of type hinting and you have a nice tidy gain to using it over not using it :)
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

If it talks and acts like a duck, it's a duck. Object hierarchies be darned.

(This means that I don't use type hinting not because of performance issues but because it gives me less flexibility in what I can pass a function. What if the object's decorated or something?)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Ambush Commander wrote:What if the object's decorated or something?
interfaces. :)
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Right. I forgot we were talking PHP 5.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

please excuse me, what is a decorated object?
it gives me less flexibility in what I can pass a function
Well yes, that is a good reason not to use type hinting. I specifically like it because I don't trust myself with flexibility. I like to limit my inputs and outputs to the smallest possible range to get the job done; and I wouldn't say I'm alone.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

please excuse me, what is a decorated object?
An object that has a decorator around it.
wei
Forum Contributor
Posts: 140
Joined: Wed Jul 12, 2006 12:18 am

Post by wei »

Most microtime benchmarks are insignificant to actual application stress tests where a great deal of improvement can be gain by finding the bottle necks using a tracer tool, and in most cases, the bottle necks are the database and disk IO.

I think more time may be wiser spent on application design and testing (of all forms) than worrying about the impact of using hinting or otherwise in terms of performance.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Agreed. XDebug + KCacheGrind/WinCacheGrind is amazingly powerful.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Type hinting would screw with a proxy too if the proxy worked by using the overloaded magic methods.... hmm... then again so would using an interface in this instance.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

I think more time may be wiser spent on application design and testing (of all forms) than worrying about the impact of using hinting or otherwise in terms of performance.
Yeeeaaahh. That'll be me then
=( : P )

I know this stuff is pretty pointless, but...


Err yeah I can't think of a but.
Post Reply