note 102378 modified in language.oop5.typehinting by danbrown
[email protected] Fri, 11 Feb 2011 06:54:17 -0800
| Newsgroups | php.notes |
|---|---|
| Message-ID | <[email protected]> |
I agree [that] 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.
--was--
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.
http://php.net/manual/en/language.oop5.typehinting.php