157 Comments
- MoneyShot, on 10/21/2007, -9/+85Bury this PLAGIARIZING *****! A good chunk of "his" tips are lifted word-for-word from a blog post I made over a year ago! http://blog.rightbrainnetworks.com/2006/09/18/10-t ...
- sfrench, on 10/19/2007, -2/+363 more tips for optimization.
1) If you have a chunk of code that you run a lot, and is slow... you might want to turn it into a compiled PHP module.
2) Have another programmer look at it. (never underestimate your own stupidity)
3) Profile yer apps (xdebug + cachegrind) - inactive, on 10/16/2007, -1/+19If you are confused by this, you belong at Digg. Stay away from Slashdot.
- proton, on 10/21/2007, -4/+18Buried because these need to be backed up with explanation. Without details they are just his (or the plagiarized source's) opinions without any foundation.
- Me1000, on 10/16/2007, -4/+18who uses the "print" command anyway?
- legendxx, on 10/16/2007, -4/+17What did you have for breakfast? I'm very interested in your daily routine.
- dotlizard, on 10/17/2007, -2/+14ok well considering he lifted the 10 tips word for word, he most likely lifted the other 30 from elsewhere, as well. what are the chances he thought up 30, then stole 10 more?
- dotdan, on 10/16/2007, -2/+13I can't verify, but why not just add the extra line anyway?
$arraySize = count($array);
for($i = 0; $i < $arraySize; $i++) { } - sfrench, on 10/19/2007, -0/+10I guess I should have referred to them by the name everybody uses; "Extensions"
http://devzone.zend.com/node/view/id/1021 - jjb123, on 10/16/2007, -0/+10Do you have more information on compiled PHP Modules? I have never heard of that before and it sounds interesting.
- Firehed, on 10/18/2007, -0/+9Please don't ever code. That's not a stupid compiler issue, that's a stupid coder issue. You tell it to count in each loop, and it does. You start adding in pseudo-optimizations of sorts where it's supposed to recognized repetitive stuff and see what goes ape *****.
- HigherLogic, on 10/16/2007, -3/+11I have a problem with #31:
"If you’re using PHP 5 with MySQL 4.1 or above, consider ditching the mysql_* functions for the improved mysqli_* functions."
mysqli_* is actually not faster or better than using mysql_* for most things. Stick with mysql_* instead. - AdamTReineke, on 10/16/2007, -0/+8If traphik's syntax was correct... Note the comma, not a semicolon, before the first $c
for($i = 0, $c = count($array); $i < $c; $i++) {}
Then, yes, this is just as efficient as dotdan's version, because the first parameter in a for loop sets up the variables. These are only set once, and not run with every iteration of the loop. - rewritereds, on 10/19/2007, -4/+12nice and simple, good read
- SPThom, on 10/17/2007, -2/+7Why not just write the whole damn thing in assembly, while you're at it?
- LucidDr34m3r, on 10/16/2007, -1/+6I can't, but it looks like the count() function still gets called in every iteration. I'd put my money on no.
- zoom1928, on 10/16/2007, -0/+5The guy's own evidence didn't support his ridiculous claim. On the page he linked to: "The data showed that mysql_query outperforms mysqli_query." That is why you never want to use the mysqli functions. Another reason is that MySQL decided to ruin the mysqli functions by not allowing them to use the query cache. Depending on your system, using the mysqli functions can bring your system to a halt. It did for several of my customers that have made that mistake.
- technoticau, on 10/16/2007, -0/+5"Useless Optimizations...
Here are some common PHP legends:
a. echo is faster than print
Echo is supposed to be faster because it doesn't return a value while print does. From my benchmarks with PHP 4.3, the difference is neglible. And under some situations, print is faster than echo (when ob_start is enabled)."
http://phplens.com/lens/php-book/optimizing-debugg ... - duniyadnd, on 10/16/2007, -0/+5if you're using header("Location:url"); make sure you follow it with a die(); SQL Injection has nothing to do with optimization (I'm thinking of speed here), but yeah, still important. Surprised there was nothing about concatenation for strings. Overall, a decent reminder list for all of us who think we remember it all.
- .Steven, on 10/18/2007, -0/+4So if I save 0.00001 seconds per page load.. and mess up my code by a factor of 10, that is a saving?
- daldredge, on 10/16/2007, -0/+4Thank You.
- jues, on 10/30/2007, -0/+4foreach ($array as $key => $value){}
- KWhat, on 10/17/2007, -0/+4Does anyone find anything wrong with #1? "If a method can be static, declare it static. Speed improvement is by a factor of 4." Ahh if you ever develop OO code please do not follow this advise!
- inactive, on 10/16/2007, -0/+4Buried for using the surpassingly stupid non-term "Web 3.0". Tard.
- makis, on 10/16/2007, -0/+4Excellent??
he first says: "Error messages are expensive"
and then at point 30 "Partially validate email addresses by checking that the domain name exists with checkdnsrr()." which is far more expensive than simply sending an email with a validation code...and more expensive than error messages too.everyone knows that dns check is not cheap at all!
So we should not use error messages but we are supposed to check email domains for dns existence?
I'm sure billg@microsoft.com is considered a valid email address for him....
but there's more
"When incrementing or decrementing the value of the variable $i++ happens to be a tad slower then ++$i. This is something PHP specific and does not apply to other languages"
it applies to almost every language that have post and pre increment operators!
++$i is faster because is l-value and $i++ is r-value, so first it returns the value of $i and then increment the variable.Every good compiler should be
able to detect situation like for(i=0;i - krazeguy, on 11/05/2007, -0/+3Turn on FastCGI
/duh - i3x171um, on 10/17/2007, -0/+3What if you add items to the array during the loop?
Though, I suppose you might counter that the compiler could look through the loop and make it a constant if it isn't ever modified. But such a small optimization probably isn't worth the thousands of lines of code it would take to implement.
Submit the patch if you want it. - mtekk, on 10/16/2007, -0/+3Unless you need the $i for something in side the loop use foreach($array as $key => $value) or foreach($array as $value) as there is no need for the extra count function at all ;)
- Chongo, on 10/21/2007, -1/+4not even 5 years ago they were teaching us to use print in college. I've learned so much more on my own then school would ever teach
- merreborn, on 10/16/2007, -0/+3"And the #1 performance boost. Make sure you're indexing your tables."
That's pretty much the #1 reason these "tips for optimizing PHP" lists are useless. The kind of people who read them spend all their time doing stupid ***** these lists suggest, shaving a few milliseconds off here and there, at best, when their *real* issues are things like deeply nested loops, unindexed databases, and other basic, yet *huge* errors.
I can promise you that not one web app has ever seen a major performance improvement from replacing "print" with "echo", or using "echo a,b,c" in place of "echo a.b.c". Changes like those are the absolute worst thing you can possibly spend your time on. - Kickboy, on 10/17/2007, -0/+3If you set variables in that manner, then they will not be set again on each iteration. So you are correct, that would be more efficient. Though your syntax is wrong as AdamTReineke pointed out. You need a comma after the $i=0 as opposed to the semi-colon.
However, in terms of coding style, it is a little more readable to just add the one extra line before the for loop as dotdan suggested.
My personal suggestion would be to just avoid using for loops for arrays anyway. Which is one point I think this article skipped over. When it comes to looping through arrays, I ALWAYS use the foreach() loop construct. It is designed specifically for arrays, and it's much more efficient than a for loop. - digger99999999, on 10/16/2007, -0/+3for($i = 0, $c = count($array); $i < $c; $i++)
Initialize multiple variables only happens at beginning of the loop they should be comma separated
As mentioned it is the same as this.
$c = count($array);
for($i = 0; $i < $c; $i++)
This method is what you want to avoid.
for($i = 0; $i < count($array); $i++) - fweeky, on 10/16/2007, -0/+3For most people, a link to http://c2.com/cgi/wiki?PrematureOptimization would probably be better.
"We should forget about small efficiencies, say about 97% of the time" -- replacing strlen() with an index lookup certainly counts. If you're trying to find the length of a string often enough for it to matter, you'll probably be much better off using a better algorithm or introducing some caching than avoiding a few function calls. Number 27 certainly deserves to be at the top. - tony23, on 10/16/2007, -1/+3I haven't tested it in PHP, but that sort of construct is significantly more efficient in JavaScript. I would have to assume for the same reasons.
- LucidDr34m3r, on 10/16/2007, -0/+2Oh, well good catch. I didn't see it was a comma. I stand corrected.
- msiekkinen, on 10/16/2007, -0/+2While the point about using single vs double quotes when possible is ok advice I hate seeing it in lists like this b/c the difference is VERY small. You would certainly be wasting time if you went around "optimizing" your code by replacing double quotes w/ single quotes. You'll probably be better off in ease of maintenance using double quotes to increase readability for strings requiring single quotes. Beside that you should never use in-string vars. It is much slower than concatenation and much more than compared w/ using single v. double.
$var="world";
$var2="Hello ".$var; - Firehed, on 10/16/2007, -0/+2Some customers care about the performance of their site. If it performs well, it reflects very well on you (which leads to being able to charge more and your skills being more in demand). Of course, it has to come after time and functionality, but it's good for more than just personal satisfaction.
- Bisqwit, on 10/16/2007, -0/+2It contains the echo+comma vs string appending thing twice, also mod_deflate vs mod_gzip which are mutually exclusive but do the same thing.
Also inaccuracies, such as $i++ vs ++$i (which contrary to article's claims applies also in C++ in some situations).
Buried as inaccurate. - Firehed, on 10/16/2007, -0/+2There's the rare occasion where it's useful, but really only with printf type things if you want to add in leading zeroes, C-style ("%-2n", $value or something - it's so rare that I use it that I always have to look it up).
- tempusrob, on 10/16/2007, -2/+4*Not* optimizing is not a bad programming practice. The OP said "don't waste your time optimizing basic PHP code" and he's right. The source of slowness in simple code is most likely external to the code itself.
Obviously there are exceptions, but if you're in a situation where performance of even basic code is top priority then you're probably not using PHP anyway. - Firehed, on 10/16/2007, -0/+2Most of them I've seen tested, and the results mirror what was posted. A single versus double-quote with echo and such is FAR faster and it's been tested and comes up as such every time. That's what happens when you aren't searching the contents for variables. But n doesn't work in singles, so you have to either tack on a double quoted n with a period or have messy html output (or, of course, just end the echo on the next line, but that's asking for trouble).
- bryxal, on 10/16/2007, -2/+4Who are the retards digging this down?? Did you guys READ the article? how about the more in depth article he links to, he shows with stats that what parent post said is true...
- makis, on 10/16/2007, -0/+2bad programming is thinking that "Incrementing an undefined local variable is 9-10 times slower than a pre-initialized one." is a real optimization...small local optimizations are often symptom of bad programming style.and most of all, 9/10 of this optimization are possible because php compiler is so stupid that doesn't even do the standard optimizations, like loop unrolling,invariant code motion,constant propagation or variable induction.the most basic ones.
- bdmbdm, on 10/17/2007, -0/+2Not to mention that the mysqli extension will automatically escape your queries for you. :)
- grantmc1, on 11/05/2007, -0/+2I am inspired to continue learning PHP, i am a designer my self xhtml/css, but this dude has gave me the inspiration to go back and learn more PHP! YEA
THAT ROCKS, don't it? - chris9902, on 10/19/2007, -0/+2glass half empty kinda guy aren't we.
- SPThom, on 10/16/2007, -1/+3Ahh... I thought it was strange that the formatting changed halfway through. It was your formatting.
- drakia, on 11/05/2007, -2/+4I'm taking it you've never done CLI based PHP?
- HigherLogic, on 11/02/2007, -2/+4What's wrong with PHP? Lots of very popular sites use it, like the one you're on now, Yahoo, etc. It's the language of the web :)
- alexdemers, on 10/16/2007, -0/+2Before you post things like this, you should make sure you're right.
Take a look at the php logo. http://www.php.net/ -
Show 51 - 100 of 157 discussions



What is Digg?