note 92914 deleted from function.array-push by danbrown

From: Date: Fri, 14 Aug 2009 18:42:12 +0000
Subject: note 92914 deleted from function.array-push by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-159486@lists.php.net to get a copy of this message
Note Submitter: sdf at gmxpro dot de ---- @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); ?>

« previous php.notes (#159486) next »