multiple instances of same class = slow
| From: | Marc Antony Vose | Date: | Tue, 17 Oct 2000 14:35:02 +0000 |
| Subject: | multiple instances of same class = slow | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-20638@lists.php.net to get a copy of this message | ||
Hi there.
I've just rewritten my publishing engine using classes so that I can remove database calls from everywhere except one central location (among other reasons). Yes, what I was doing before was a bit messy (but it was fast).
The basic idea is this:
On the bottom is a simple class which gets stuff from a table or tables and puts it into an array called stuff.
A bunch of classes extend that class, and they are organized by the way I tend to view data (i.e., 'product', 'company', 'discussion thread' and so on). Basically, they handle building a SQL query which the base class uses to snag data.
On top of that, various template pages call those classes and spit out the data formatted into HTML or XML or plain text depending on the target client.
This is a simple example of how it works in practice in the template files:
$this_product = new MGDBproduct;
$where_clause = "WHERE
products.id=$id
AND products.publisher=companies.id";
$this_product->SpecifyWhereClause($where_clause);
$this_product->GetData();
then all your data is in the array
$this_product->stuff[0]["column_1"]
$this_product->stuff[0]["column_2"]
$this_product->stuff[0]["column_3"]
etc.
and you can loop that array if necessary (for multiple returned records).
NOW, after implementing this, i didn't notice a speed hit from the Web client, and I was happy. That is, until I happened on a page that needed to have two instances of the same class.
Here are two examples from my development server:
http://dev.gamedb.com/ssps/0/1/00131
http://dev.gamedb.com/ssps/0/1/00609
Guess which one uses two instances of the MGDBproduct class and, hence, seems to take a significant speed hit. The stupid thing is, all i need the second instance for is to retrieve the information in the tiny "Related Products" link at the side (and all it retrieves is the id number and name).
It's exactly the same template file (product.php). The PHP code says "If this game has a dependency on another game, then create a second instance of the MGDBproduct class, and fill it with the id number and name of that second game". So the faster one is skipping that second class instance entirely, and is running, it seems, in about 1/10th of the time.
BTW, there are several other classes also called in that same page; it's only the duplicate of the same class that seems to be causing the speed hit.
So, does this just go with the territory in PHP or is there something I can do to speed it up? Like maybe some configuration option somewhere?
I'm using PHP4.0.0 compiled as a module into Apache 1.3.12.
I don't know if the Zend Optimizer would speed this sort of thing up, but I can't use it anyway, as I'm on LinuxPPC and they don't support it.
Baffled,
--
Marc Antony Vose
http://www.suzerain.com/
Into every tidy scheme for arranging the pattern of human life, it is
necessary to inject a certain dose of anarchism.
-- Bertrand Russell