IT Community - Software Programming, Web Development and Technical Support

Which is faster for array, while or for or foreach ?

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...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Web Development > PHP Programming

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 07-20-2007, 06:13 AM
vivekanandan vivekanandan is offline
D-Web Trainee
 
Join Date: Jul 2007
Posts: 41
vivekanandan is on a distinguished road
Default Which is faster for array, while or for or foreach ?

Hi,
For Processing N dimension array, what type of looping statement is effective
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-20-2007, 07:15 AM
Senthilkumar Senthilkumar is offline
D-Web Programmer
 
Join Date: Mar 2007
Posts: 93
Senthilkumar is on a distinguished road
Default Re: Which is faster for array, while or for or foreach ?

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.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


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


All times are GMT -7. The time now is 02:12 AM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0