PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old October 29th, 2005, 12:57 PM
IBBoard IBBoard is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 62 IBBoard User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 h 5 m 12 sec
Reputation Power: 6
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

Reply With Quote
  #2  
Old October 29th, 2005, 05:10 PM
SimonGreenhill's Avatar
SimonGreenhill SimonGreenhill is offline
(retired)
Dev Shed God 11th Plane (10000 - 10499 posts)
 
Join Date: Dec 2003
Location: The Laboratory
Posts: 10,101 SimonGreenhill User rank is General 5th Grade (Above 100000 Reputation Level)SimonGreenhill User rank is General 5th Grade (Above 100000 Reputation Level)SimonGreenhill User rank is General 5th Grade (Above 100000 Reputation Level)SimonGreenhill User rank is General 5th Grade (Above 100000 Reputation Level)SimonGreenhill User rank is General 5th Grade (Above 100000 Reputation Level)SimonGreenhill User rank is General 5th Grade (Above 100000 Reputation Level)SimonGreenhill User rank is General 5th Grade (Above 100000 Reputation Level)SimonGreenhill User rank is General 5th Grade (Above 100000 Reputation Level)SimonGreenhill User rank is General 5th Grade (Above 100000 Reputation Level)SimonGreenhill User rank is General 5th Grade (Above 100000 Reputation Level)SimonGreenhill User rank is General 5th Grade (Above 100000 Reputation Level)SimonGreenhill User rank is General 5th Grade (Above 100000 Reputation Level)SimonGreenhill User rank is General 5th Grade (Above 100000 Reputation Level)SimonGreenhill User rank is General 5th Grade (Above 100000 Reputation Level)SimonGreenhill User rank is General 5th Grade (Above 100000 Reputation Level)SimonGreenhill User rank is General 5th Grade (Above 100000 Reputation Level)  Folding Points: 4925 Folding Title: Novice Folder
Time spent in forums: 3 Months 3 Weeks 5 h 49 m 4 sec
Reputation Power: 1331
Facebook
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

Reply With Quote
  #3  
Old October 29th, 2005, 05:40 PM
IBBoard IBBoard is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 62 IBBoard User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 h 5 m 12 sec
Reputation Power: 6
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?

Reply With Quote
  #4  
Old November 7th, 2005, 02:07 PM
IBBoard IBBoard is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 62 IBBoard User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 h 5 m 12 sec
Reputation Power: 6
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

Reply With Quote
  #5  
Old November 7th, 2005, 02:57 PM
kicken's Avatar
kicken kicken is offline
Wiser? Not exactly.
Dev Shed Specialist (4000 - 4499 posts)
 
Join Date: May 2001
Location: Ft Myers, FL
Posts: 4,205 kicken User rank is Colonel (50000 - 60000 Reputation Level)kicken User rank is Colonel (50000 - 60000 Reputation Level)kicken User rank is Colonel (50000 - 60000 Reputation Level)kicken User rank is Colonel (50000 - 60000 Reputation Level)kicken User rank is Colonel (50000 - 60000 Reputation Level)kicken User rank is Colonel (50000 - 60000 Reputation Level)kicken User rank is Colonel (50000 - 60000 Reputation Level)kicken User rank is Colonel (50000 - 60000 Reputation Level)kicken User rank is Colonel (50000 - 60000 Reputation Level)kicken User rank is Colonel (50000 - 60000 Reputation Level)kicken User rank is Colonel (50000 - 60000 Reputation Level)kicken User rank is Colonel (50000 - 60000 Reputation Level)  Folding Points: 117001 Folding Title: Super Ultimate Folder - Level 1Folding Points: 117001 Folding Title: Super Ultimate Folder - Level 1Folding Points: 117001 Folding Title: Super Ultimate Folder - Level 1Folding Points: 117001 Folding Title: Super Ultimate Folder - Level 1Folding Points: 117001 Folding Title: Super Ultimate Folder - Level 1Folding Points: 117001 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 1 Month 3 Weeks 9 h 44 m 42 sec
Reputation Power: 573
Send a message via ICQ to kicken Send a message via AIM to kicken Send a message via MSN to kicken
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://wiser.aoeex.com/ - Long term project (offline due to evil crawlers and lack of content)
http://www.aoeex.com/gmap.php - Put yourself on the map (Now Updated!)

Reply With Quote
  #6  
Old November 8th, 2005, 02:38 PM
IBBoard IBBoard is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 62 IBBoard User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 h 5 m 12 sec
Reputation Power: 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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPHP Development > Optimisation of loops and understandinf php.lt benchmark page


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
Stay green...Green IT