note 101478 deleted from function.gettype by sobak

From: Date: Fri, 02 Dec 2016 08:17:42 +0000
Subject: note 101478 deleted from function.gettype by sobak
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-207940@lists.php.net to get a copy of this message
Note Submitter: Crysis nerd ---- 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

« previous php.notes (#207940) next »