Forum Moderators: coopster

Message Too Old, No Replies

Making this more efficient

PHP DB vs File for data

         

Atharva

8:50 am on Apr 12, 2008 (gmt 0)

10+ Year Member



Hi,
I am trying to speed up my website.
My website is a simple readonly information website and data is stored in an include file. The include file consists of a php array which has all the data. array size varies between 25 to 50 elements for a page.

I opted for this approach instead of reading data from DB is:
DB can sometimes be down or tables might crash
DB might lie on another server and connecting, reading tables might take a little time.

Is there a more better way to speed up my app, like using static php array or any other caching include file techniques which would involve minimal code changes.

Thanks

coopster

4:59 pm on Apr 12, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You shouldn't be having any issues with an array that small. I have loaded arrays with much, much more than that. Are you having performance issues with arrays this small?

mikhaill

7:10 pm on Apr 12, 2008 (gmt 0)

10+ Year Member



What I do in such situations is combine the two methods.

<?
//setup a cache system so not every call is quiried to the DB
$file = 'table.txt';
$expire = 43200; // 12 hours
if (file_exists($file) &&
filemtime($file) > (time() - $expire)) {
$faq = unserialize(file_get_contents($file));
}
else
{

$faq = array of data retreived from the DB
$OUTPUT = serialize($faq);
$fp = fopen($file,"w");
fputs($fp, $OUTPUT);
fclose($fp);
} // end else
?>
Then you just do whatever you want with the $faq variable. What this does is optimize your time. Instead of having to edit the php file every time you edit the value in your array, you edit the Database and then delete the cache file. The cache file is then regenerated every 12 hours (or whatever you set it to).

Atharva

5:21 pm on Apr 13, 2008 (gmt 0)

10+ Year Member



Thanks mikhaill for that

Coopster, basically as i said i have no performance issue but i want to speed up my site. currently it takes 5 secs for a user see page load, i want to get it down to 3 ! Are there any good debugging scripts which would help me find out where are the bottlenecks ?

jatar_k

11:55 am on Apr 14, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



take a look at images (size and where they're stored), css and js files, extra html could also make a difference when you are trimming down that last bit

you need to look at the whole picture

maybe making some more pages completely static

MattAU

8:56 pm on Apr 14, 2008 (gmt 0)

10+ Year Member



I'd recommend not using serialize for caching, it's a very slow... If you need to save array or object data it's a lot quicker to write your own function for it.

Some PHP IDEs have decent built in debuggers, they generally aren't free though. I use PhpED which is pretty good.

A quick and dirty way of debugging is to print out the time taken between code blocks / sections. Once you've found a bottleneck block you can time the individual functions and lines to see if there's anything problematic.