Jump to content

Efficiency questions


jbreits

Recommended Posts

Just wondering what everyone else's thoughts are on these efficiency quetsions. I use a shared hosting plan and I'm afraid that when I go live with my site I will end up eating up too many resources on the server. Of course, I don't know that it will be a problem, but I still want my site to be as efficient as possible.

1. I have several places where the same SQL query is being run. For instance, if a person is viewing a post on my site, the query runs with each page load. The query is not overly complex although it does use 3 tables through 2 joins. Since the content in the post is not likely to change very often, I thought maybe it would be a good idea to cache the query results to a file. Then, when any user loads the page, it looks to the cache file for the results. If the data ever changes, I would flush the cache. Is it worth doing caching in this case or would it be quicker to query the DB? I currently use caches for search results, but these queries are usually more complex, involve pagination, and I actually don't want the search results to update while they are viewing them. I'm just trying to figure out what the threshold is for when caching becomes quicker than querying. If so, how would one handle concurrent access to a 'global' cache file? It seems that concurrent access would only be a problem when the cache was being written initially. After that, it is just read-only.

2. I have been building my site trying to separate the presentation from the processing by putting the processing routines in 'api' files and including those api files in my .php files that are being shown to the user. However, one api file is starting get kind of large (~2000 lines). If I have another file include this api file, but the script really only needs 2-3 functions out of the api file, isn't that wasted resources, or will the other functions not be parsed unless they are used?

Thanks,
JBreits

Thought of a couple more.

3. I have seen that is more efficient to break out of PHP mode for large blocks of static HTML rather than echoing the whole thing out. But if one would be breaking out of PHP to output just a few lines, would that be more efficient still, or would it be best to echo them?

4. Generally speaking, how much time is too much time for a page to be generate on the server? I know that the internet connection can be a bottleneck but I'm not really worried about that as there is nothing I can do about it.


Thanks,
JBreits
Link to comment
Share on other sites

If you're very concerned about efficiency I suggest you start to use Smarty and probably a PEAR DB package as well such as MDB2. Smarty is a template engine that let's you cache "compiled" templates along with many other benefits. This means if you have a page that doesn't change very often (not the search result page most likely) you can just have it display the cache instead of re-compile it each. (This is handled mostly automatically)

In terms of the API's that you spoke of. I think you really need to evaluate them and ask yourself whether any of them can broken into different objects. Exactly how closely related are all of these functions? Chances are many of them could be in their own classes thereby reducing the include file size for any one file.

Finally I suggest you try the benchmark PEAR package so that you have a firm numbers on the speed of things. You can set markers throughout your page and know exactly how long it takes to get from A to B to C. This can help you shave off bloated code in a more targeted fashion.

Hope you find some of that helpful, Buyo.
Link to comment
Share on other sites

Thank you for your suggestions. As far as templating goes, I'm not really interested in using a templating system, as I have written one myself. Basically, I write my templates and data files locally, then I run them through a custom application that I wrote which outputs all the files based on the templates. This gives me files with as much static html as possible. The stuff that is dynamic (the PHP portions) obviously are still have to be evaluated at the server. I figure using this method is even more efficient than a templating package. It probably uses a little more disk space, but that's minimal because it's text.

I am currently using the PHP mysql functions for database access. From what I see on this forum, the mysql functions are probably the fastest for DB access. For caching search results, I wrote my own routines which I think work very well and are quite efficient.

You are right on the API's they could be broken up. I wasn't sure if I needed to, but it looks like I should. Currently, everything is procedural but I could switch to using objects. I've heard that objects are not as efficient as procedural code, but maybe it would be worth switching for usability.

I have downloaded the benchmark package from the PEAR website. I will see if I can figure out how to use it. I don't really understand what PEAR is at this point. What exactly does PEAR do? I have apache2triad installed on my test box and I know that has PEAR installed on it. Does my hosting company have to have PEAR installed in order for me to use PEAR packages on their server?

Thanks,
JBreits
Link to comment
Share on other sites

You can make PEAR as easy or hard as you want :) I suggest that you download the benchmark package. Unzip it into the folder you downloaded it to. Open that folder and make sure you see the contents you want. I believe you need Timer.php maybe one other thing. Rename the directory that Timer.php etc are in to something more simple like Benchmark then upload that directory to your server. Then at the top of a script you want to time include the Timer.php file...
include ('Benchmark/Timer.php');
then you do something like...
$timer = new Timer();
$timer->start();

$timer->setMarker("marker 1");

$timer->stop();

something like that. Anyway there are some examples included in the download, try looking at them. Let me stress, YOU DO NOT NEED TO INSTALL ANYTHING. Just include the files needed. Hope that helps.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.