note 102378 deleted from language.oop5.typehinting by cmb
| From: | cmb@php.net | Date: | Sat, 10 Dec 2016 11:43:39 +0000 |
| Subject: | note 102378 deleted from language.oop5.typehinting by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-208439@lists.php.net to get a copy of this message | ||
Note Submitter: macobex dot exe at gmail dot com
----
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.