Folks,
Ever since reading an interesting article in Linux Format on PHP whereby
suggested code writing was made that could enhance performance on a server,
I've started testing various bits of code everytime I found more than one
method to perform a single task. I timed each method to find which would
complete faster. I thought I'd share my most recent results which (I
believe) should help those write their programs to be more
clean/environmentally friendly on systems resources.
If you don't want to read further, basically, I created two identical tasks
except one used a "for" loop and the other used "foreach" loop. I found
"for" loops are almost ten percent faster to use when compared to "foreach"
loops when working on arrays.
My problem (or task pending which way you want to look at it):
I had a problem whereby I had a string that I wanted to cut to x number of
words - I wanted the cut to be clean (thus not cut in the middle of a word,
but cut between words)... My solution was to convert the string in to an
array, take each array element (a single word) and append it to a new
string - then check the length of the new string and make sure it had not
exceeded maximum length. Maximum length was dynamic because sometimes I
wanted the first ten (whole) words - sometimes more...
While reading the temporary array (that contained the words), I knew I could
perform a foreach($tmpArray as $wordCount=>$aWord) or I could perform a
"for" loop that incremented a numeric variable to the maximum number of
elements in the array of words.
Call me sad - but I then wondered if using foreach on an array was
lighter/heavier on the system then using a for loop... so I put both of them
to the test. My test script below records the speed of which would complete
first - a for loop, or a foreach loop when both were required to perform the
same routine inside the loop...
I discovered that using a for loop is about ten percent faster than using a
foreach loop. A foreach loop took 45seconds on my server to perform the
same test that took 42seconds using a for loop... It might not be much, but
every little bit helps, especially if you have several users on your system
at the same time drawing on resources used by PHP.
I'm interested in any comments anyone has to offer on this...
<?
// Execute each test $maxLoop number of times
$maxLoop=25;
// Our test array will contain
$maxArraySize=200000;
/////////////////////////////////////////////////////////////////////
// Create an array, $maxArrayLength elements in size - each element
// in the array will contain random strings
set_time_limit(100);
for($tmpA=0; $tmpA<$maxArraySize; ++$tmpA)
{ $randomString=mt_rand(0,$maxArraySize) . time();
$tmpArray[]=md5($randomString);
}
/////////////////////////////////////////////////////////////////////
// Select a random value from the array
$randomElement=round(mt_rand(0,$maxArraySize));
$randomValue=$tmpArray[$randomElement];
/////////////////////////////////////////////////////////////////////
// Start test 1
set_time_limit(100);
print("<br>Search for $randomElement $randomValue<br>");
$start=time();
for($tmpA=0; $tmpA<$maxLoop; ++$tmpA)
{ foreach($tmpArray as $key=>$value)
{ if("$value"=="$randomValue")
{ break; }
}
}
$endTest1=time()-$start;
/////////////////////////////////////////////////////////////////////
// Start test 2
set_time_limit(100);
$start=time();
for($tmpA=0; $tmpA<$maxLoop; ++$tmpA)
{ for($tmpB=0; $tmpB<$maxArraySize; ++$tmpB)
{ $value=$tmpArray[$tmpB];
if("$value"=="$randomValue")
{ break; }
}
}
$endTest2=time()-$start;
/////////////////////////////////////////////////////////////////////
print("<hr>Test 1 = $endTest1 seconds");
print("<hr>Test 2 = $endTest2 seconds<br>");
?>
--
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet?