note 102378 added to language.oop5.typehinting

[email protected] Thu, 10 Feb 2011 18:48:22 -0800
Newsgroups php.notes
Message-ID <[email protected]>
I agree to gdecad about using object/wrapper is faster than using the is_* functions.

But I think we must also consider the fact that when we pass an argument to a function, most likely we will use it's value. So I've done a little test to see which approach is faster.

Primitive Approach:
<?php

function testPrimitive($str) {
   if ( is_string($str) )
      $x = $str . ' world';   // using the argument's value
   else
      trigger_error('Argument passed must be of type string.');
   }

?>

Wrapper Approach:

<?php

   function testWrapper(StringWrapper $str) {
      $x = $str->getValue() . ' world';  // using the argument's value
   }

?>

Test: 10,000 iterations
Results:
   primitive average: 0.026113033294678
   wrapper average: 0.063039064407349

   primitive is faster than wrapper approach by 41.42357368431%

Using the is_* function is faster than using wrapper class, given the fact that we just not want to check the type of the argument, but we also want to use the value of the argument.
----
Server IP: 69.147.83.197
Probable Submitter: 112.202.163.142
----
Manual Page -- http://www.php.net/manual/en/language.oop5.typehinting.php
Edit        -- https://master.php.net/note/edit/102378
Del: integrated  -- https://master.php.net/note/delete/102378/integrated
Del: useless     -- https://master.php.net/note/delete/102378/useless
Del: bad code    -- https://master.php.net/note/delete/102378/bad+code
Del: spam        -- https://master.php.net/note/delete/102378/spam
Del: non-english -- https://master.php.net/note/delete/102378/non-english
Del: in docs     -- https://master.php.net/note/delete/102378/in+docs
Del: other reasons-- https://master.php.net/note/delete/102378
Reject      -- https://master.php.net/note/reject/102378
Search      -- https://master.php.net/manage/user-notes.php