note 89440 added to language.types.null

From: Date: Sun, 08 Mar 2009 00:59:07 +0000
Subject: note 89440 added to language.types.null
Groups: php.notes 
Request: Send a blank email to php-notes+get-151611@lists.php.net to get a copy of this message
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 ---- Server IP: 217.75.128.29 Probable Submitter: 78.90.43.45 ---- Manual Page -- http://www.php.net/manual/en/language.types.null.php Edit -- https://master.php.net/note/edit/89440 Del: integrated -- https://master.php.net/note/delete/89440/integrated Del: useless -- https://master.php.net/note/delete/89440/useless Del: bad code -- https://master.php.net/note/delete/89440/bad+code Del: spam -- https://master.php.net/note/delete/89440/spam Del: non-english -- https://master.php.net/note/delete/89440/non-english Del: in docs -- https://master.php.net/note/delete/89440/in+docs Del: other reasons-- https://master.php.net/note/delete/89440 Reject -- https://master.php.net/note/reject/89440 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#151611) next »