note 92914 added to function.array-push

From: Date: Fri, 14 Aug 2009 14:00:52 +0000
Subject: note 92914 added to function.array-push
Groups: php.notes 
Request: Send a blank email to php-notes+get-159475@lists.php.net to get a copy of this message
@bryan: willdemaine iterates 10 times less the in the last two loops because pushes 10 times more items per loop onto the array. @willdemaine: i tried it myself with 1.000.000 loops and without your class, and "$array[] = " seems quite faster than array_push. OUTPUT: Adding 1.000k elements to array with [] 0.46351408958435 s Adding 1.000k elements to array with array_push 0.69087219238281 s Adding 1.000k elements to array with [] 10 per iteration 0.26789498329163 s Adding 1.000k elements to array with array_push 10 per iteration 0.27218985557556 s CODE: <?php echo "Adding 1.000k elements to array with []\n\n"; $ta = array(); $start = microtime(true); for ($i = 0; $i < 1000000; $i++) { $ta[] = $i; } echo microtime(true) - $start, ' s'; unset($ta, $start); echo "\n\nAdding 1.000k elements to array with array_push\n\n"; $ta = array(); $start = microtime(true); for ($i = 0; $i < 1000000; $i++) { array_push($ta,$i); } echo microtime(true) - $start, ' s'; unset($ta, $start); echo "\n\nAdding 1.000k elements to array with [] 10 per iteration\n\n"; $ta = array(); $start = microtime(true); for ($i = 0; $i < 100000; $i++) { $ta[] = $i; $ta[] = $i; $ta[] = $i; $ta[] = $i; $ta[] = $i; $ta[] = $i; $ta[] = $i; $ta[] = $i; $ta[] = $i; $ta[] = $i; } echo microtime(true) - $start, ' s'; unset($ta, $start); echo "\n\nAdding 1.000k elements to array with array_push 10 per iteration\n\n"; $ta = array(); $start = microtime(true); for ($i = 0; $i < 100000; $i++) { array_push($ta,$i,$i,$i,$i,$i,$i,$i,$i,$i,$i); } echo microtime(true) - $start, ' s'; unset($ta, $start); ?> ---- Server IP: 212.124.37.9 Probable Submitter: 91.0.100.133 ---- Manual Page -- http://www.php.net/manual/en/function.array-push.php Edit -- https://master.php.net/note/edit/92914 Del: integrated -- https://master.php.net/note/delete/92914/integrated Del: useless -- https://master.php.net/note/delete/92914/useless Del: bad code -- https://master.php.net/note/delete/92914/bad+code Del: spam -- https://master.php.net/note/delete/92914/spam Del: non-english -- https://master.php.net/note/delete/92914/non-english Del: in docs -- https://master.php.net/note/delete/92914/in+docs Del: other reasons-- https://master.php.net/note/delete/92914 Reject -- https://master.php.net/note/reject/92914 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#159475) next »