note 103855 deleted from function.end by danbrown

From: Date: Fri, 06 May 2011 21:32:48 +0000
Subject: note 103855 deleted from function.end by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-179509@lists.php.net to get a copy of this message
Note Submitter: montana ---- some notes on Tim C's method using foreach is far faster: For 2000 keys twice as fast, for 20,000 keys it takes 1/5 of the time as the array keys method... the more keys, the more benefit the foreach has. This could be due to the skipped action of building an intermediary set with array_keys [I believe arrays allocates new memory for all of the keys in the original array, which is unnecessary in this case.] test: <?php function getTime () { list($usec, $sec) = explode(" ", microtime()); $time = ((float)$usec + (float)$sec); return $time ; } // * * * $id = 'testindex'; $busters = array(); for ($a=0;$a<22000;$a++) { $busters[$id.$a] = $id.$a; } $start = getTime(); //using foreach foreach($busters as $buster=>$keaton) {} //get last element $end = getTime(); $total = $end - $start; echo "\r\n"; echo "For Each Render time: " . $total; echo "\r\n"; echo "\r\n"; $start = getTime(); //array pop / keys method $buster = array_pop(array_keys($busters)); $end = getTime(); $total = $end - $start; echo "\r\n"; echo "Array Keys Render time: " . $total; echo "\r\n"; echo "\r\n";

« previous php.notes (#179509) next »