Init.php and a Question of Speed
11 posts by 6 authors in: Forums > CMS Builder
Last Post: June 4, 2010 (RSS)
By Chris - June 4, 2010
Hi everyone,
The best advice I've ever gotten and I can give is to profile before you even think about optimizing. Profiling is the process of measuring how fast something is, preferably compared to something else. Many programmers are plagued by the problem of "premature optimization" -- spending ages trying to make something faster without first researching whether it's slow in the first place!
The worst thing you can do is spend a week or more of your own time trying to speed something up for only an imperceptable gain (or, in some cases, the system might even be slower -- yes, this happens.)
If you want to see how much your PHP code is slowing down your page serving, copy the source code of a page (from your browser) and paste it into an HTML file, then upload that alongside your PHP file. You can then benchmark the two pages (preferably using an automated timer and many tests.) Now you know how much time your PHP is costing you.
If your HTML isn't lightning fast, you may be able to speed up both! There are many ways to do this, such as organizing your CSS/JS includes, putting JS at the bottom, etc. See the YSlow plugin and writeup for some excellent ideas. These kinds of changes are pretty easy to make and will speed up your dynamically generated PHP pages too! On a previous project at another company, I cut our page load times down by two thirds using the YSlow recommendations. No amount of PHP optimization could have come close to those results!
You can also try knocking out certain PHP functionality from your pages to see what each feature is costing you. If you find a feature on your page that's costing you a disproportionate amount of time, post your code and ask if there's a more efficient way to do it.
Another way to cheaply speed up your page loads might be to try a host with faster servers.
I wouldn't be afraid of foreach loops, unless they're "nested" inside each other. Then again, often that's the most efficient way to do things. :)
"init.php" files are common practice for web development as well as pretty much all software development. Other languages call them static libraries.
The best advice I've ever gotten and I can give is to profile before you even think about optimizing. Profiling is the process of measuring how fast something is, preferably compared to something else. Many programmers are plagued by the problem of "premature optimization" -- spending ages trying to make something faster without first researching whether it's slow in the first place!
The worst thing you can do is spend a week or more of your own time trying to speed something up for only an imperceptable gain (or, in some cases, the system might even be slower -- yes, this happens.)
If you want to see how much your PHP code is slowing down your page serving, copy the source code of a page (from your browser) and paste it into an HTML file, then upload that alongside your PHP file. You can then benchmark the two pages (preferably using an automated timer and many tests.) Now you know how much time your PHP is costing you.
If your HTML isn't lightning fast, you may be able to speed up both! There are many ways to do this, such as organizing your CSS/JS includes, putting JS at the bottom, etc. See the YSlow plugin and writeup for some excellent ideas. These kinds of changes are pretty easy to make and will speed up your dynamically generated PHP pages too! On a previous project at another company, I cut our page load times down by two thirds using the YSlow recommendations. No amount of PHP optimization could have come close to those results!
You can also try knocking out certain PHP functionality from your pages to see what each feature is costing you. If you find a feature on your page that's costing you a disproportionate amount of time, post your code and ask if there's a more efficient way to do it.
Another way to cheaply speed up your page loads might be to try a host with faster servers.
I wouldn't be afraid of foreach loops, unless they're "nested" inside each other. Then again, often that's the most efficient way to do things. :)
"init.php" files are common practice for web development as well as pretty much all software development. Other languages call them static libraries.
All the best,
Chris
Chris