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

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 May 26th, 2004, 09:08 AM
Admin Admin is offline
Developer Shed
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2004
Posts: 938 Admin User rank is Sergeant Major (2000 - 5000 Reputation Level)Admin User rank is Sergeant Major (2000 - 5000 Reputation Level)Admin User rank is Sergeant Major (2000 - 5000 Reputation Level)Admin User rank is Sergeant Major (2000 - 5000 Reputation Level)Admin User rank is Sergeant Major (2000 - 5000 Reputation Level)Admin User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Article Discussion: Writing Clean and Efficient PHP Code

If you've ever had to go back to an application you wrote after an extended period of time, you already know the value of clean, well documented and efficient code. But how can you make your code better? Here are some tips that'll help you speed up and clean up your development cycle.


Read the full article here: Writing Clean and Efficient PHP Code

Reply With Quote
  #2  
Old May 26th, 2004, 04:54 PM
factory21 factory21 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2004
Posts: 1 factory21 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
DB Looping

Overall, not a bad article and the section on string literals helped me improve my code a bit; I'm pretty used to C/Java conventions regarding string literals and use double quotes out of habit.

However, I'm sort of scratching my head with the following example regarding looping over db results:

$r = mysql_query('SELECT * FROM someTable LIMIT 1500');
//OPTION 1
for ($i = 0; $i < mysql_num_rows($r); $i++) {
print mysql_result($r, $i, 'ColA').mysql_result($r, $i, 'ColB');
}

// OPTION 2
while (($row = mysql_fetch_assoc($r)) !== false) {
print $row['ColA'].$row['ColB'];
}

The article states:
>As you may guess, "OPTION 1" runs significantly faster. It has lower memory and machine...

I use option2-like methods almost exclusively in my code and also in all my other db code (C++/Java/MS Access). Not only does it look cleaner to me, but all indicators point to it being FASTER than option1 for large sets of data.

From the PHP docs for mysql_result:
When working on large result sets, you should consider using one of the functions that fetch an entire row (specified below). As these functions return the contents of multiple cells in one function call, they're MUCH quicker than mysql_result(). Also, note that specifying a numeric offset for the field argument is much quicker than specifying a fieldname or tablename.fieldname argument.


I did some simple benchmarking and my results reflected the PHP docs:
  1. In cases where 1 row was returned and using only 1 column, mysql_result (option1) was faster by a smidge.
  2. In cases where many rows (1500 as in the example), mysql_fetch_row (option2) was about 15% faster when only using 1 column from the row.
  3. In cases where many rows were returned and 2 columns were used, mysql_fetch_row was on the order of 100% faster.

Reply With Quote
  #3  
Old May 26th, 2004, 07:02 PM
crazytrain81 crazytrain81 is offline
Always Learning
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Oct 2002
Location: Port Neches, TX, USA
Posts: 1,173 crazytrain81 User rank is Corporal (100 - 500 Reputation Level)crazytrain81 User rank is Corporal (100 - 500 Reputation Level)crazytrain81 User rank is Corporal (100 - 500 Reputation Level)crazytrain81 User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 3 Days 7 h 45 m 43 sec
Reputation Power: 11
Send a message via AIM to crazytrain81 Send a message via MSN to crazytrain81
You are correct - I'm not sure how I overlooked that when I profread but I intended to say 'Despite what you may guess, "OPTION 2" runs significantly faster' when i said 'As you may guess, "OPTION 1" runs significantly faster'.

The part about the iterator was intended to lead the reader along the path you would take when trying to reason out which would be faster, but then point out that it isn't correct =)

I'll make sure it gets corrected; thanks for pointing it out! I feel like a moron for missing that =D.
BTW, for fastest results you can use fetch_row (w/o specifying it to be an associative) and it will be even faster using subscripts only rather than keys, but it relies heavily on you knowing what values are in what positions in the array, which can be a pain for debugging.
__________________
David Fells
If my post helped you, please click the above my post and leave a comment. Thanks

Last edited by crazytrain81 : May 26th, 2004 at 07:04 PM.

Reply With Quote
  #4  
Old May 27th, 2004, 06:07 AM
kurious's Avatar
kurious kurious is offline
Prom night: 1973
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Dec 2001
Posts: 1,152 kurious User rank is Corporal (100 - 500 Reputation Level)kurious User rank is Corporal (100 - 500 Reputation Level)kurious User rank is Corporal (100 - 500 Reputation Level)kurious User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 1 Week 2 Days 7 h 36 m 22 sec
Reputation Power: 11
Quote:
Originally Posted by crazytrain81
BTW, for fastest results you can use fetch_row (w/o specifying it to be an associative) and it will be even faster


I think "even faster" is misleading. mysql_fetch_assoc:
Quote:
An important thing to note is that using mysql_fetch_assoc() is not significantly slower than using mysql_fetch_row(), while it provides a significant added value.

Reply With Quote
  #5  
Old May 27th, 2004, 10:04 AM
crazytrain81 crazytrain81 is offline
Always Learning
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Oct 2002
Location: Port Neches, TX, USA
Posts: 1,173 crazytrain81 User rank is Corporal (100 - 500 Reputation Level)crazytrain81 User rank is Corporal (100 - 500 Reputation Level)crazytrain81 User rank is Corporal (100 - 500 Reputation Level)crazytrain81 User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 3 Days 7 h 45 m 43 sec
Reputation Power: 11
Send a message via AIM to crazytrain81 Send a message via MSN to crazytrain81
It is significant with huge data sets =)

Reply With Quote
  #6  
Old May 27th, 2004, 03:23 PM
gnorb gnorb is offline
Banned
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Location: Davie, FL
Posts: 0 gnorb User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via AIM to gnorb
Fixed

Reply With Quote
  #7  
Old May 28th, 2004, 08:35 AM
pbwebguy pbwebguy is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2004
Posts: 1 pbwebguy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
If we are talking optimization, then you should remove any unnecessary function calls from the loop (i.e. mysql_num_rows($r)).

Instead of:

for ($i = 0; $i < mysql_num_rows($r); $i++) {

It should be:

for ($i = 0, $c=mysql_num_rows($r); $i < $c; $i++) {

Reply With Quote
  #8  
Old May 28th, 2004, 11:09 AM
crazytrain81 crazytrain81 is offline
Always Learning
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Oct 2002
Location: Port Neches, TX, USA
Posts: 1,173 crazytrain81 User rank is Corporal (100 - 500 Reputation Level)crazytrain81 User rank is Corporal (100 - 500 Reputation Level)crazytrain81 User rank is Corporal (100 - 500 Reputation Level)crazytrain81 User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 3 Days 7 h 45 m 43 sec
Reputation Power: 11
Send a message via AIM to crazytrain81 Send a message via MSN to crazytrain81
pbwebguy, good catch =)

Reply With Quote
  #9  
Old May 31st, 2004, 05:21 AM
Phex Phex is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2004
Posts: 2 Phex User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 m 22 sec
Reputation Power: 0
Nice article. I learned some new information.

I have a suggestion for Part 2: "Function development in PHP". The article could describe rules of creating fuctions. TNX!

Reply With Quote
  #10  
Old May 31st, 2004, 05:49 PM
andnaess andnaess is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Jul 2001
Location: Oslo
Posts: 1,516 andnaess User rank is Private First Class (20 - 50 Reputation Level)andnaess User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 9
I'm quite surprised that this article actually made it past the editor (is there one at all?). I haven't read many of the articles here at DevShed, but I hope this one doesn't represent the general quality.

The article should be about writing clean and efficient code, yet it mostly deals with efficiency. And it's dealings with this subject are poor at best. There is not a single piece of datum supporting the author's claims. And the tips presented are completrely irrelevant. Does the author know about the 80/20 rule? Has he ever heard the word "profiler"? Has he read even the most basic introduction to performance tuning?

The single vs. double quote example says it all. Maybe there is a difference. It's quite possible. But does it matter? NO! Trust me, you will have to search for a long time to find a PHP application where echoing stuff to the browser takes up a significant amount of time. And this was under the header "Best practices"!

It seems to as if the author really doesn't think about what he is claiming. He's just writing things that sound good. To quote from the conclusion: "How clean your code is can be determined by measuring the efficiency and maintainability of the application code"... Sure. So how *does* one measure the maintainability of code? And I'm not very convinced that the author knows how to measure efficiency.

Oh, and C++ doesn't have "wretched garbage collection". C++ has no garbage collection.

Then again, this article gives added credibility to the claim I once heard that writers of books about programming are people who weren't clever enough to make it as programmers
__________________
--
Regards
André Nęss

Puritanism: The haunting fear that someone, somewhere may be having fun

Reply With Quote
  #11  
Old June 2nd, 2004, 02:35 PM
crazytrain81 crazytrain81 is offline
Always Learning
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Oct 2002
Location: Port Neches, TX, USA
Posts: 1,173 crazytrain81 User rank is Corporal (100 - 500 Reputation Level)crazytrain81 User rank is Corporal (100 - 500 Reputation Level)crazytrain81 User rank is Corporal (100 - 500 Reputation Level)crazytrain81 User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 3 Days 7 h 45 m 43 sec
Reputation Power: 11
Send a message via AIM to crazytrain81 Send a message via MSN to crazytrain81
andnaess, pretty harsh =)

after i read the article on the site i really was not happy at all with it... i didn't collect my thoughts at all before writing it and it shows. i'm a newbie at writing so what i intend to say may not come out as well in the article as it does in my head when i'm thinking things through =)

your points are fair but there's not any need to get nasty in the future i'll follow my gut and just not allow articles i'm not happy with to be posted.

Reply With Quote
Reply

Viewing: Dev Shed ForumsOtherDevelopment Articles > Article Discussion: Writing Clean and Efficient PHP Code


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 |