 |
06-22-2008, 08:32 PM
|
PHP Optimization
|
Posts: 984
Name: Jeremy Miller
Location: Reno, NV
|
I'm always trying to improve my programming skills, as many of you know. I though it'd be cool to open a thread on PHP optimzation. What tips do you have?
Here's a link which discusses a number of tips to get us started: http://reinholdweber.com/?p=3
|
|
|
|
06-23-2008, 11:44 AM
|
Re: PHP Optimization
|
Posts: 1,036
Name: Abel Mohler
Location: Asheville, North Carolina USA
|
I'm probably not advanced enough to bring up any really cool stuff in this area, but have you ever heard of the Zend Optimizer and the Zend Guard? Together, they are a PHP compression toolkit that speeds up complex scripts on the server by compressing then interpreting them. Here is a link to the optimizer.
The only downside is that it must be installed manually from the server, which means that if the host does not include it, you may be up a creek if you need it to interpret a compressed script.
|
|
|
|
06-23-2008, 12:43 PM
|
Re: PHP Optimization
|
Posts: 738
|
Great idea, Jeremy. Interesting resource, too. However, I think it's also important to to talk about and see actual test results and the tests that were run. It makes it easier to visualize and also to see that the advice is valid. (Just yesterday) I found an interesting resource that does that, though it's far from comprehensive: http://www.phpbench.com/.
Perhaps it would also be good to point out that PHP optimization is a topic in its own right and it doesn't necessarily have anything to do how quickly your database runs or how quickly your web page loads. Other than normalization, if you want to optimize your database, because each database is different, you should consult its respective manual. Some useful website optimization resources are the Web Page Analyzer, YSlow, and Cuzillion.
Getting back to PHP optimization, can we really talk about it without also talking about testing? A lot of the blogs and resources that I've read recommend writing your code so that it works first and then optimizing iteratively through testing. Testing is one area that I haven't explored heavily enough yet, but the above advice makes sense to me. It's good to know some basics, but too much pre-optimization could get in the way, especially if your code changes a lot in its early stages.
That said, I'd love to find out more about testing. I know that each test suite probably works a little different, but if anyone has any recommendations that are easy to use, I'd love to hear them. Ditto on building your own tests.
|
|
|
|
06-23-2008, 12:56 PM
|
Re: PHP Optimization
|
Posts: 984
Name: Jeremy Miller
Location: Reno, NV
|
That's a great link to PHP Bench. I don't have time to look at it in detail right now, though, but I will later.
And, I totally agree about testing: You can't have a conversation like this without doing some testing. I usually write my own tests. I'm sure there's stuff out there to do it, though.
|
|
|
|
06-25-2008, 11:20 AM
|
Re: PHP Optimization
|
Posts: 738
|
So does anyone have any resources for testing? I've heard of a few: Selenium, Simple Test, and PHPUnit; but I'd love to hear about people's experience with them and if there are others out there that you recommend. I'm also interested in benchmark tests as well.
|
|
|
|
06-25-2008, 02:57 PM
|
Re: PHP Optimization
|
Posts: 5,199
Name: John Alexander
|
Quote:
Originally Posted by JeremyMiller
I'm always trying to improve my programming skills, as many of you know. I though it'd be cool to open a thread on PHP optimzation. What tips do you have?
|
- Write efficient algorithms. This is the #1 way to optimize any code. Move code out of loops that doesn't need to be there, etc.
- Use the optimal data types for the job at hand.
- Know your server! Memory is a valuable resource, orders of magnitude faster than the disc. How much RAM is available to your application? What are the other characteristics of the box that you need to know, in order to figure out the best performance way to approach a given problem? Is the box CPU bound? Is your database on the same physical machine?
- Prefer Hashtable objects to arrays when you'll need to do non iterating lookups.
- Use strict typing and disallow implicit conversions.
- Acquire resources late, and give them up early.
- Be very aware of any locks you're taking, whether they're mutexes and semaphores, or inherent in database calls.
- Avoid virtual functions when possible.
- Don't hit the disc if you don't have to.
- Cache whatever you can.
And so on. To the best of my knowledge, tho, PHP can't be compiled. It's always a script, text being parsed and then interpreted as it gets run. If that's true, optimization is something of a lost cause. Compiled code is a must have for a high performance system. You would have to work very hard to write a compiled program that runs slower than a similar, but interpreted one. If you have any way to run compiled code in production, that will make as much difference as all honest programming mistakes combined.
|
|
|
|
06-25-2008, 03:05 PM
|
Re: PHP Optimization
|
Posts: 5,199
Name: John Alexander
|
Quote:
Originally Posted by VirtuosiMedia
Great idea, Jeremy. Interesting resource, too. However, I think it's also important to to talk about and see actual test results and the tests that were run.
|
This can be tremendously helpful, and many people will advise against "premature optimization". But it isn't necessary throughout much of an application. The first rules of writing performant code are that we need to write efficient algorithms, use appropriate data types, and so on - we don't need integration testing to know whether this is the case.
Say I need to know what 5 + 7 are. There are any number of ways I could approach this problem. Let's look at one of them. - Tell PHP to tell MySQL to make a new table, called JohnsTempMathTable, with a numeric column called TheValue.
- Tell PHP to tell MySQL to add a constraint to TheValue forcing integer values - no decimals.
- Tell PHP to fire off an insert query adding the value 5 to my table.
- Tell PHP to fire off an insert query adding the value 7 to my table.
- Tell PHP to query MySQL for the sum of all rows in JohnsTempMathTable.
- Fetch and consume the data.
Now, an alternate way to get about the answer is to create an int variable and populate it with 5 + 7.
Do you need to test the software to figure out which is the better approach? For this particular case, does performance testing give you any useful data that looking at the code wouldn't? I would say no. In fact, it's a waste of time to do perf testing before this issue is corrected. I think you can't deliver a mature application without lots of perf testing, but I have to disagree that the proper way to do this that you have to wait until a module is finished before thinking about its performance footprint.
|
|
|
|
06-25-2008, 04:19 PM
|
Re: PHP Optimization
|
Posts: 738
|
Quote:
Originally Posted by Learning Newbie
Do you need to test the software to figure out which is the better approach? For this particular case, does performance testing give you any useful data that looking at the code wouldn't? I would say no. In fact, it's a waste of time to do perf testing before this issue is corrected. I think you can't deliver a mature application without lots of perf testing, but I have to disagree that the proper way to do this that you have to wait until a module is finished before thinking about its performance footprint.
|
I think that the link that Jeremy gave was offering some general advice about different techniques to practice from the beginning (before performance testing) that will result in better performance. My point was simply that even though those practices may be faster, I'd like empirical evidence with side by side comparisons to prove that they are actually faster, something more in line with the link I provided. It was more for general coding practices than application specific. But I agree with you, I don't think absolutely everything needs to be tested. At a certain point, it becomes a time-sink.
|
|
|
|
|
« Reply to PHP Optimization
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|
|