note 89440 deleted from language.types.null by danbrown

From: Date: Sun, 08 Mar 2009 01:03:48 +0000
Subject: note 89440 deleted from language.types.null by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-151612@lists.php.net to get a copy of this message
Note Submitter: kshegunov at gmail dot com ---- As <james> stated is_null is slower than ===, but the catch is that his test is wrong. Here is the correct one: <?php function mean($data) { return array_sum($data) / count($data); } function deviation($data, $mean) { $squares = 0; foreach ($data as $value) $squares += pow($value - $mean, 2); return sqrt($squares / count($data)); } $v = NULL; $isnull = array(); $eqoperator = array(); for ($j = 0; $j < 50; $j++) { // Single test: is_null $s = microtime(TRUE); for($i = 0; $i < 1e6; $i++) is_null($v); $isnull[] = microtime(TRUE) - $s; // Single test: === $s = microtime(TRUE); for($i = 0; $i < 1e6; $i++) $v === NULL; $eqoperator[] = microtime(TRUE) - $s; } $isnull_avg = mean($isnull); $eqoperator_avg = mean($eqoperator); $isnull_dev = deviation($isnull, $isnull_avg); $eqoperator_dev = deviation($eqoperator, $eqoperator_avg); printf('is_null > %.3f ± %.3f (from %.3f to %.3f)<br>', $isnull_avg, $isnull_dev, $isnull_avg - $isnull_dev, $isnull_avg + $isnull_dev); printf('=== > %.3f ± %.3f (from %.3f to %.3f)', $eqoperator_avg, $eqoperator_dev, $eqoperator_avg - $eqoperator_dev, $eqoperator_avg + $eqoperator_dev); ?> Results: is_null > 0.414 ± 0.006 (from 0.408 to 0.420) === > 0.201 ± 0.002 (from 0.199 to 0.203) So as you can see === is faster not by factor of 30, but by factor of 2. Moreover this test is done for 50 test groups with one million iterations each, which leads to the conclusion that it is not an issue. Be smart, optimize your algorithms and don't worry that is_null is a little slower than the identity operator. Tested on: Athlon X2 64bit 5600+ / php 5.2.6 / WinXP x64

« previous php.notes (#151612) next »