|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Optimisation of loops and understandinf php.lt benchmark page
Hi,
I'm trying to optimise my custom CMS software (just for a personal project, triggered by the fact that my host has had to remove some spammers recently who really overloaded the server - made me decide to see how well I could drop my server load )Anyway, I've checked SQL statements for using indexes and I've never done any of the sillier things like for loops with the count() not done before the loop, so now I'm looking at my foreach's. I found http://www.php.lt/benchmark/phpbench.php, which seems to imply that instead of doing foreach($array as $key=>$value) it's better to do array_keys($array) and then use a for loop to go through the keys. Am I reading that right? Is that the fastest way when you need the key and the value? because I read that page a couple of times and at first I couldn't quite get what it was meaning in the very very small example! Thanks |
|
#2
|
||||
|
||||
|
Something like that is a seriously trivial optimisation, and probably not worth doing unless you're really desperate.
Have a look here and at my post here for some ideas of useful optimisation techniques. --Simon
__________________
|
|
#3
|
|||
|
|||
|
Looking at the results on the php.lt benchmark page it didn't seem like trivial improvements. Granted the site uses larger indexes and keys then I tend to use, but every time I view the page it seems to say that foreach performs the loop in about 1,000% of the time of the fastest loop, and "$key = array_keys($aHash);$size = sizeOf($key);for ($i=0; $i<$size; $i++)" runs in about 150% of the time of the fastest. I don't count mysql row fetching (because the bias of calculating the time it took outweighs the time it actually took) but my site currently runs on about 80% PHP time and 20% MySQL time, so I got the feeling that it was my PHP that needed more optimisation (skins.hiveworldterra.com if you want to check the current load and generation times).
I've found the phplens article and read it earlier - I already do most of the ideas, or else don't have control because I'm on shared hosting. My host already runs Zend, I already use ob_start (with GZip for the bandwidth saving) and I already do most of the optimisations as standard 'best coding practice'. One thing that I am planning to do next time I have time is to hunt down any glaring loop invariants or any easily-flattened functions. Thanks for pointing me to the thread as well (don't think it turned up in my search). I might look into the spiders later, but at the moment they don't bother me - I'm going for performance increases for my own reasons, not trying to fight against very high server load. At some point I might write my own static-file-creation section for the site, but for now it can wait. Thanks for the ideas. Does anyone else have an explaination of the php.lt code? |
|
#4
|
|||
|
|||
|
Hi,
Does anyone else have any ideas? As I said, I'm doing just about all of the obvious MySQL and other optimisations that always get mentioned. I've got a test page to try and work out what the php.lt benchmarks are doing (code follows) but that seems to be showing that a foreach is faster than either of my two methods of grabbing the keys/values and foring through them. Code:
$arr = array();
for ($i = 0; $i<10000; $i++)
{
$key = substr(md5($i),0,mt_rand(2,32));
$arr[$key] = substr(md5($i).md5($i),0,mt_rand(5,50));
}
$temp = array();
$output = "<table><tr><td>";
$str1 = "";
$time1 = microtime();
foreach ($arr AS $k=>$v){
$temp[$k] = $v;
}
$time2 = microtime();
$temp = array();
$str2 = "";
$time3 = microtime();
$keys = array_keys($arr);
$a = microtime();
$vals = array_values($arr);
$b = microtime();
$count = count($keys);
$c = microtime();
for ($i = 0; $i<$count; ++$i){
$temp[$keys[$i]] = $vals[$i];
}
$time4 = microtime();
$str3 = "";
$keys = array();
$vals = array();
$temp = array();
$time5 = microtime();
$keys = array_keys($arr);
$count = count($keys);
for ($i = 0; $i<$count; ++$i){
$temp[$keys[$i]] = $arr[$keys[$i]];
}
$time6 = microtime();
$mtime = explode (' ', $time1);
$starttime = $mtime[1] + $mtime[0];
$mtime = explode (' ', $time2);
$endtime = $mtime[1] + $mtime[0];
echo sprintf("%01.5f",($endtime - $starttime));
echo "<br />\r\n ";
$mtime = explode (' ', $time3);
$starttime = $mtime[1] + $mtime[0];
$mtime = explode (' ', $time4);
$endtime = $mtime[1] + $mtime[0];
echo sprintf("%01.5f",($endtime - $starttime));
echo "<br />\r\n";
$mtime = explode (' ', $time5);
$starttime = $mtime[1] + $mtime[0];
$mtime = explode (' ', $time6);
$endtime = $mtime[1] + $mtime[0];
echo sprintf("%01.5f",($endtime - $starttime));
echo "<br />\r\n",$time3," | ",$a," | ",$b," | ",$c;
The $a, $b and $c are in there to bench-mark how long the array_keys and array_values takes. So, any ideas or suggestions? Or anyone know the code that is being used in those examples? Thanks |
|
#5
|
||||
|
||||
|
I know recently (I think in php5) the PHP team optimized the way the foreach loop works and gained a considerable speed boost on it. I'd just use a foreach rather than any other method for simplicities sake.
For what its worth, the way you did a foreach prior to it existing (the php3 way) was to use while/each(). Code:
while (list($key, $value) = each ($array)){
}
You could try that if you want, but again I'd just stick with foreach()
__________________
Spidermonkey Tutorial http://www.aoeex.com/gmap.php - Put yourself on the map (Now Updated!) |
|
#6
|
|||
|
|||
|
Thanks for the idea, but that actually ends up the same, if not slightly slower, on the php.lt benchmarks
Obviously the old PHP3 way got improved on with foreach.With my benchmark, I got all sorts of random results on my little dev server, but then it's a 1.2Gig Celeron laptop so you can't really trust its times! After uploading it to my hosting server, I'm wondering whether Zend has some special optimisations for foreach loops - my foreach seems to be completing in about two thirds of the time of the array_keys()/array_values()/count() method and about three quarters of the time of the array_keys()/count() method. Maybe it even has something to do with the fact that I'm not using such huge data values or something (mine are generally going to be a few bytes up to maybe 1KB on a rare occasion. Nevermind, unless someone can give me some faster code (something as fast as that benchmark page seems to indicate) then I think I'll stick to rummaging for other optimisations. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > PHP Development > Optimisation of loops and understandinf php.lt benchmark page |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|