General Chat
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Codewalkers ForumsGeneralGeneral Chat

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 Codewalkers Forums Sponsor:
  #1  
Old December 6th, 2002, 09:18 PM
CmdrDats CmdrDats is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: <br><img src='http://www.dats.co.za/icon.gif'>
Posts: 269 CmdrDats User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 3
Send a message via ICQ to CmdrDats Send a message via AIM to CmdrDats Send a message via Yahoo to CmdrDats
Interesting piece of php trivia

Did you know that foreach($ar as $key=>$val) {} is two times faster than the while(list($key,$val) = each($ar)) alternative? *gasp* i can't imagine why i insisted on using while(...) to iterate thru my arrays...

Reply With Quote
  #2  
Old December 7th, 2002, 01:41 AM
Matt Matt is offline
Contributing User
Codewalkers Specialist (4000 - 4499 posts)
 
Join Date: Apr 2007
Location: Florida
Posts: 4,158 Matt User rank is Private First Class (20 - 50 Reputation Level)Matt User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 4 h 12 m 16 sec
Reputation Power: 7
RE: Interesting piece of php trivia

That is interesting..I guess the extra function calls just cause the overhead...here's my results:

Code:
foreach: 4.04250204563
while: 7.35851705074


php Code:
Original - php Code
  1. <?php
  2.  
  3. function getmicrotime(){
  4.    list($usec, $sec) = explode(" ",microtime());
  5.    return ((float)$usec + (float)$sec);
  6. }
  7.  
  8. $data = array_fill(1,1000000,'foo');
  9.  
  10.  
  11. $time_start = getmicrotime();
  12. reset($data);
  13. foreach($data as $key=>$val);
  14. $time_end = getmicrotime();
  15. $time = $time_end - $time_start;
  16. echo "foreach: $time<br />n";
  17.  
  18. $time_start = getmicrotime();
  19. reset($data);
  20. while(list($key,$val) = each($data));
  21. $time_end = getmicrotime();
  22. $time = $time_end - $time_start;
  23. echo "while: $time<br />n";
  24.  
  25. ?>

Reply With Quote
  #3  
Old December 7th, 2002, 01:54 AM
JWright JWright is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Manlius, NY, USA
Posts: 17 JWright User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to JWright Send a message via AIM to JWright Send a message via Yahoo to JWright
RE: Interesting piece of php trivia

I thought I was just being lazy by using foreach()...

Josh

Reply With Quote
  #4  
Old December 7th, 2002, 02:07 AM
zombie zombie is offline
Codewalkers Intermediate (1500 - 1999 posts)
 
Join Date: Apr 2007
Location: serbia
Posts: 1,876 zombie User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 4
RE: Interesting piece of php trivia

why is this so surprising to you? when some funcion is built in the language, it means that it is implemented in C, so it has to be faster.

php doesn't have to have so many functinos, especialy string and array functions, since all or most of them can be done in php, but one of the reasons it does, is because their implementation is oh-so-much-faster than in php...

Reply With Quote
  #5  
Old December 7th, 2002, 07:57 PM
CmdrDats CmdrDats is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: <br><img src='http://www.dats.co.za/icon.gif'>
Posts: 269 CmdrDats User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 3
Send a message via ICQ to CmdrDats Send a message via AIM to CmdrDats Send a message via Yahoo to CmdrDats
RE: Interesting piece of php trivia

i just found it strange, coz foreach() makes a copy of the array (so two foreach()'s inside each other working on the same array will work, and while() wont) so i assumed, quite wrongly, that it has to be slower than just resetting the array..

another cool thing is that with foreach() you don't have to use reset()..

yeah, i forgot to take into account the compiled code advantage ;) shoot me.

Reply With Quote
  #6  
Old December 7th, 2002, 08:17 PM
zombie zombie is offline
Codewalkers Intermediate (1500 - 1999 posts)
 
Join Date: Apr 2007
Location: serbia
Posts: 1,876 zombie User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 4
RE: Interesting piece of php trivia

BANG!

Reply With Quote
  #7  
Old December 8th, 2002, 08:17 AM
CmdrDats CmdrDats is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: <br><img src='http://www.dats.co.za/icon.gif'>
Posts: 269 CmdrDats User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 3
Send a message via ICQ to CmdrDats Send a message via AIM to CmdrDats Send a message via Yahoo to CmdrDats
RE: Interesting piece of php trivia

ahh.. That feels much better, thanks.

Reply With Quote
Reply

Viewing: Codewalkers ForumsGeneralGeneral Chat > Interesting piece of php trivia


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




 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

Request Your Free Technology Downloads!
 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

Request Your Free Technology Downloads!
 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

Request Your Free Technology Downloads!
 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

Request Your Free Technology Downloads!
 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

Request Your Free Technology Downloads!
 

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




© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
For more Enterprise Application Development news, visit eWeek