Steps to Reduce CPU Load in php scripts?

Hiya, I have now reached the point in my web development life where i now need to be careful how I construct page to allow for minimal CPU load.
My host suspended an account today cos some of my pages were suing too much CPU with the high traffic volume i’m getting these days :smiley:

The scripts i have are very basic, using flatfiles to store paragraphs of text and part of html code for inclusion to a php page.

My guess is that its the reading/writing of the flatfiles (use flatfile caches to) that is eating up the CPU. IF i’m correct should I convert my flatfile function into MYSQL queries, then write the results into a static html page, and perhaps have a cron function to write new updated pages every few hours?

Does this sound like a plan, or am I going up the wrong path again?

Thanks for any advice on input you can provide :smiley:

MySQL would be a good idea.

I don’t think static HTML pages would be much use until traffic gets extremely high; MySQL is actually faster than file access IIRC.

If you’re getting high traffic, don’t you think it’s about time you moved to a private server?

its not high traffic to you big guys, but its high to me hehe. he did suggest moving to a VPS, but of course he would! When i start pulling in more consistent £ then i’ll upgrade for sure.
I will go down the mysql route, and create an option to write to html file, and then my host can compare the performance or each over a period of time.

edit: origianl post should say higher traffic lol!

what about the size of variables? for instance is it ok to have $text = paragraphs and paragraphs of text?

That’s perfectly fine.

More often than not, it’s code inefficiencies, based on needless over-processing.

If you’re willing to post some example code, we might be able to show you where your innefficiencies may be, if any.

make sure you REALLY need to upgrade.
I had host telling me my sites were using to much CPU, and that I need to upgrade VPS when I barely had any traffic on them. (and they said nothing when i had 10x the traffic for a few months)

You can try to cache your data, I’m sure you have parts of your pages that get generated with an output that does not change.

I have a feeling actually suspending the account is different :wink:

optimize your sql, it will reduce the cpu load

i dont have any sql at the moment its all flatfiles :cool:

got the pen and paper out planning my tables though :shifty:

I was about to say. :lol:

If you’re exceeding your usage quota on a small site with little traffic, your hosting company’s iron grip on their wallet might be the real problem.

Well if you come across any problems we’re all here to help.

Hi Sensaay,

Its quite hard to help reduce the CPU profile of your scripts without actually seeing the code, You could setup http://www.xdebug.org/ with [URL=“http://kcachegrind.sourceforge.net/html/Home.html”]http://kcachegrind.sourceforge.net/html/Home.html these tools are a great way to fine tune the performance of your scripts/applications by profiling with Xdebug. When configured correctly, Xdebug will output trace files that you can check with kcachegrind or wincachegrind to find potential bottlenecks. There are a number of steps you’ll need to take to setup your environment before you can use these tools - if you’re running Windows, forget it, you will need a *nix flavor of some description.