note 101478 added to function.gettype
| From: | Crysisnerd at osu1 dot php dot net | Date: | Sun, 19 Dec 2010 14:40:26 +0000 |
| Subject: | note 101478 added to function.gettype | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-175056@lists.php.net to get a copy of this message | ||
I wanted to compare the type of 2 vars.
1st method: Use this Funktion (gettype) and compare with ==
2nd method: Compare every type per is_[type]
Here a little Benchmark:
(I had to break line twice..)
<?php
$var1 = "Hallo";
$var2 = "Käsekuchen";
$var3 = 3.141526;
$Start = microtime(true);
for($i=0;$i<10000;$i++)
{
if(is_bool($var1)&&is_bool($var2)||is_int($var1)&&
is_int($var2)||is_string($var1)&&is_string($var2)||
is_object($var1)&&is_object($var2)||is_array($var1)
&&is_array($var2))
{
//same
}
if(is_bool($var3)&&is_bool($var2)||is_int($var3)&&
is_int($var2)||is_string($var3)&&is_string($var2)||
is_object($var3)&&is_object($var2)||is_array($var3)
&&is_array($var2))
{
//same
}
}
$time = microtime(true)-$Start;
echo "Zeit fuer if abfrage: ".$time."<br>";
$Start = microtime(true);
for($i=0;$i<10000;$i++)
{
if(gettype($var1)==gettype($var2))
{
//same
}
if(gettype($var3)==gettype($var2))
{
//same
}
}
$time = microtime(true)-$Start;
echo "Zeit fuer funktion : ".$time;
?>
Output:
Zeit fuer if abfrage: 0.0257611274719
Zeit fuer funktion : 0.0139331817627
So the function isnt so slow...
Lukas
----
Server IP: 217.160.235.204
Probable Submitter: 82.149.163.205
----
Manual Page -- http://www.php.net/manual/en/function.gettype.php
Edit -- https://master.php.net/note/edit/101478
Del: integrated -- https://master.php.net/note/delete/101478/integrated
Del: useless -- https://master.php.net/note/delete/101478/useless
Del: bad code -- https://master.php.net/note/delete/101478/bad+code
Del: spam -- https://master.php.net/note/delete/101478/spam
Del: non-english -- https://master.php.net/note/delete/101478/non-english
Del: in docs -- https://master.php.net/note/delete/101478/in+docs
Del: other reasons-- https://master.php.net/note/delete/101478
Reject -- https://master.php.net/note/reject/101478
Search -- https://master.php.net/manage/user-notes.php