Page 1 of 1

Performance Implications of Type Hinting

Posted: Wed Aug 02, 2006 6:14 am
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
)

Posted: Wed Aug 02, 2006 7:56 am
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)

Posted: Wed Aug 02, 2006 8:07 am
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
)
:)

Posted: Wed Aug 02, 2006 8:18 am
by Ollie Saunders
typeHinted is 24.74% slower than notTypeHinted
and instanceof is 24.16% slower than typeHinted.

Posted: Wed Aug 02, 2006 8:31 am
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 :)

Posted: Wed Aug 02, 2006 5:03 pm
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?)

Posted: Wed Aug 02, 2006 5:07 pm
by feyd
Ambush Commander wrote:What if the object's decorated or something?
interfaces. :)

Posted: Wed Aug 02, 2006 5:07 pm
by Ambush Commander
Right. I forgot we were talking PHP 5.

Posted: Wed Aug 02, 2006 5:20 pm
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.

Posted: Wed Aug 02, 2006 5:28 pm
by Ambush Commander
please excuse me, what is a decorated object?
An object that has a decorator around it.

Posted: Wed Aug 02, 2006 6:24 pm
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.

Posted: Wed Aug 02, 2006 6:25 pm
by Ambush Commander
Agreed. XDebug + KCacheGrind/WinCacheGrind is amazingly powerful.

Posted: Wed Aug 02, 2006 6:36 pm
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.

Posted: Wed Aug 02, 2006 6:46 pm
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.