This is a discussion on Which is faster for array, while or for or foreach ? within the PHP Programming forums, part of the Web Development category; Hi, For Processing N dimension array, what type of looping statement is effective...
| |||||||
| Register | FAQ | Members List | Calendar | Mark Forums Read |
| |||
| CHeck the below sample code <? function microtime_float() { list($usec, $sec) = explode(" ", microtime()); return ((float)$usec + (float)$sec); } function createRandomString($len = 8) { $chars = "abcdefghijkmnopqrstuvwxyz023456789"; srand((double)microtime()*1000000); $i = 0; $pass = '' ; while ($i <= $len) { $num = rand() % 33; $tmp = substr($chars, $num, 1); $pass = $pass . $tmp; $i++; } return $pass; } $max_element = 1000; // Initialize array, filling random keys and values for ($i = 0; $i <= $max_element; $i++) { $test_array[createRandomString(8)] = createRandomString(64); } function ForEachLoop() { global $test_array; foreach ($test_array as $ele) { $tmp[] = $ele; } } function WhileListEachLoop() { global $test_array; while (list(, $ele) = each($test_array)) { $tmp[] = $ele; } } function ForEachLoopWithKey() { global $test_array; foreach ($test_array as $k=>$ele) { $tmp[] = $ele; } } function WhileListEachLoopWithKey() { global $test_array; while (list($k, $ele) = each($test_array)) { $tmp[] = $ele; } } function GetKeyLoop() { global $test_array; $keys = array_keys($test_array); $count = sizeOf($keys); for ($i=0; $i<$count; $i++) { $tmp[] = $test_array[$keys[$i]]; } } $tests = array( 'ForEachLoop', 'WhileListEachLoop', 'ForEachLoopWithKey', 'WhileListEachLoopWithKey', 'GetKeyLoop' ); $test_score = array(); foreach ($tests as $test) { // Foreach test for ($a=1; $a<=5; $a++) { reset($test_array); $timestart = microtime_float(); call_user_func($test); $time_took = microtime_float() - $timestart; $test_score[$test][] = $time_took*1000; } } ?> <table cellpadding=4 border=0 cellspacing=2> <tr> <th>Loop style</th> <th>Test 1</th> <th>Test 2</th> <th>Test 3</th> <th>Test 4</th> <th>Test 5</th> <th>Average</th> </tr> <? foreach ($tests as $test) { ?> <tr> <td><?=$test; ?></td> <? foreach ($test_score[$test] as $score) { ?> <td align=right><?=number_format($score, 2); ?></td> <? } ?> <td align=right><?=number_format(array_sum($test_score[$test])/sizeof($test_score[$test]), 2); ?></td> </tr> <? } ?> </table> The performance of foreach function is best when compared with other array loop execution functions Cheers Senthil Kumar S. |
![]() |
| Thread Tools | |
| Display Modes | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| What’s the difference between the System.Array.CopyTo() and System.Array.Clone() ? | Archer | C# Programming | 2 | 08-25-2007 03:00 AM |
| Why preincrement operator is faster than postincrement? | Sabari | C and C++ Programming | 1 | 07-24-2007 04:42 AM |
| Difference in for & foreach, exec | vadivelanvaidyanathan | Perl | 0 | 07-15-2007 07:16 PM |
| A Faster Wi-Fi World Is Coming | vigneshgets | The Lounge | 0 | 05-29-2007 06:16 AM |
| Making Code Faster | pranky | C and C++ Programming | 1 | 03-02-2007 12:17 AM |