Folks,
A ng poster recently questioned their usage/creation of arrays and their
correct syntax. I got the idea to performance test from a recent
(excellent) PHP Tutorial article that was in Linux Format magazine (which
dealt with performance).
The original poster had a reply from someone who had said using
$testArray[$keyName] was better (and proper) than $testArray["$keyName"]
I *had* believed this to be correct... but... against my better judgement, I
decided to test the assumption.
I did a simple test to confirm - Basically, a loop that performed a
conditional 'if' test a large number of times - One used the double quotes
around the array key, one went without. Other than that, the rest was the
same - the box was almost asleep too during the test with a single user and
no cron jobs or other activity. To make sure, I ran the script twice, ten
minutes apart.... and the results?
Strangely, the test *with* the double quotes took half the time. We're
talking about Test 1 taking 85seconds and test 2 taking 152seconds
I'm curious on anything anyone can add to this - It got me thinking that if
something is faster, is it right/recommended practice?
My test script is below....
--
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?
<?
// Perform a test $maxLoop number of times, count the number of seconds
// it takes to complete the loop - Test 1 has the array element named
// inside double quotes, Test 2 excludes the double quotes
set_time_limit(250);
$testArray=$_ENV;
$maxLoop=100000;
/////////////////////// Test 1 //////////////////// This took 85seconds to
complete
$start=time();
for($loopMany=0; $loopMany<$maxLoop; ++$loopMany)
{ foreach($testArray as $elementName=>$elementValue)
{ if($testArray["$elementName"]=="HOST")
{ $thisHost="$elementValue"; }
}
}
$end=time();
$difference=$end-$start;
print("<br>Duration 1: $difference<hr>");
set_time_limit(250);
$testArray=$_ENV;
$maxLoop=100000;
/////////////////////// Test 2 //////////////////// This took 151 seconds to
complete
for($loopMany=0; $loopMany<$maxLoop; ++$loopMany)
{ foreach($testArray as $elementName=>$elementValue)
{ if($testArray[$elementName]=="HOST")
{ $thisHost="$elementValue"; }
}
}
$end=time();
$difference=$end-$start;
print("<br>Duration 2: $difference<hr>");
?>
--
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?