note 81177 added to function.in-array

From: Date: Sun, 17 Feb 2008 07:51:07 +0000
Subject: note 81177 added to function.in-array
Groups: php.notes 
Request: Send a blank email to php-notes+get-136216@lists.php.net to get a copy of this message
@vidmantas I had different results... Here is an example: <?php $array = array(); for ($x=1;$x <= 1000;$x++): $array[] = mt_rand(100000,999999); endfor; $value = 123456; $time=microtime(1); if (in_array($value,$array)): echo "Found! "; else: echo "Not found! "; endif; $time=number_format(microtime(1)-$time, 6); echo "Time to complete: " . $time; echo "<br />"; // ------------------------------------- $time=microtime(1); $array2 = array_flip($array); if (isset($array2[$value])): echo "Found! "; else: echo "Not found! "; endif; $time=number_format(microtime(1)-$time, 6); echo "Time to complete: " . $time; ?> RESULT: Not found! Time to complete: 0.000075 Not found! Time to complete: 0.000259 So I would say that on PHP5 at least, in_array seems to be more effective. ---- Server IP: 69.147.83.197 Probable Submitter: 71.229.228.181 ---- Manual Page -- http://www.php.net/manual/en/function.in-array.php Edit -- https://master.php.net/note/edit/81177 Del: integrated -- https://master.php.net/note/delete/81177/integrated Del: useless -- https://master.php.net/note/delete/81177/useless Del: bad code -- https://master.php.net/note/delete/81177/bad+code Del: spam -- https://master.php.net/note/delete/81177/spam Del: non-english -- https://master.php.net/note/delete/81177/non-english Del: in docs -- https://master.php.net/note/delete/81177/in+docs Del: other reasons-- https://master.php.net/note/delete/81177 Reject -- https://master.php.net/note/reject/81177 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#136216) next »