note 100543 added to language.oop5.typehinting
| From: | gdecad at NOSPAM dot example dot com | Date: | Fri, 22 Oct 2010 13:02:49 +0000 |
| Subject: | note 100543 added to language.oop5.typehinting | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-173371@lists.php.net to get a copy of this message | ||
I have made a little bench between three method of type hinting for native type (string, integer,
...).
First method : by test type in function like :
<?php
function testTest($arg) {
if (!is_string($arg)) {
trigger_error('Argument $arg passed to test must be an instance of string, other
given');
}
return $arg;
}
?>
Second method : by object representing native type :
<?php
function testObject(StringObj $arg) {
return $arg;
}
?>
Third method : by class TypeHint proposed by Daniel :
<?php
function testHint(string $arg) {
return $arg;
}
?>
the results are here :
bench for 100000 iterations, in seconds
avg min max total
test 5.3275489807129E-6 2.8610229492188E-6 0.0033020973205566 0.53275895118713
object 4.9089097976685E-6 3.814697265625E-6 0.0025870800018311 0.49089503288269
hint 3.2338891029358E-5 2.9802322387695E-5 0.0025920867919922 3.2338931560516
As you can see, the method by object is the best
now you know...
----
Server IP: 69.147.83.197
Probable Submitter: 83.206.36.129
----
Manual Page -- http://www.php.net/manual/en/language.oop5.typehinting.php
Edit -- https://master.php.net/note/edit/100543
Del: integrated -- https://master.php.net/note/delete/100543/integrated
Del: useless -- https://master.php.net/note/delete/100543/useless
Del: bad code -- https://master.php.net/note/delete/100543/bad+code
Del: spam -- https://master.php.net/note/delete/100543/spam
Del: non-english -- https://master.php.net/note/delete/100543/non-english
Del: in docs -- https://master.php.net/note/delete/100543/in+docs
Del: other reasons-- https://master.php.net/note/delete/100543
Reject -- https://master.php.net/note/reject/100543
Search -- https://master.php.net/manage/user-notes.php