note 70778 added to function.array-shift

From: Date: Fri, 27 Oct 2006 15:35:20 +0000
Subject: note 70778 added to function.array-shift
Groups: php.notes 
Request: Send a blank email to php-notes+get-119185@lists.php.net to get a copy of this message
Note that array_shift() can be rather time consuming. Whenever possible, you should consider using array_slice() instead. Consider the following code : $monthlyHits = 0; reset ($hitsArray); foreach($hitsArray as $visitTime ) { if ($visitTime < $monthStart ) { array_shift($hitsArray); $monthlyHits++; } } This could be replaced by : $monthlyHits = 0; reset ($hitsArray); foreach($hitsArray as $visitTime ) { if ($visitTime < $monthStart ) { $monthlyHits++; } } $monthlyHits = array_slice($hitsArray,$monthlyHits); Here is a benchmark I did on a 10 000 rows array : First method, 9000 mili-seconds. Second method, 4 mili-seconds ---- Server IP: 194.246.101.61 Probable Submitter: 195.154.42.119 ---- Manual Page -- http://www.php.net/manual/en/function.array-shift.php Edit -- https://master.php.net/note/edit/70778 Del: integrated -- https://master.php.net/note/delete/70778/integrated Del: useless -- https://master.php.net/note/delete/70778/useless Del: bad code -- https://master.php.net/note/delete/70778/bad+code Del: spam -- https://master.php.net/note/delete/70778/spam Del: non-english -- https://master.php.net/note/delete/70778/non-english Del: in docs -- https://master.php.net/note/delete/70778/in+docs Del: other reasons-- https://master.php.net/note/delete/70778 Reject -- https://master.php.net/note/reject/70778 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#119185) next »