views:

118

answers:

3

I have a web application written in PHP. It uses MySQL for data storage. Today I decided to profile it to find bottlenecks in code and find which parts of it are running slower that the others. The usual stuff. I did a lot of work and now my page loads in less than 0.05 seconds on my desktop.

But now my profiler tells me that half of this time my app is busy constructing classes. Front controller requires Config, Database and User classes, they have their own stuff to do in __construct(), then it loads Page controller, which loads Cache and View, and after that it fires the main() method of Page. And all that stuff takes 50% of total working time. And the other 50% are used for querying db, juggling query results and outputting them in View.

The question is: is this okay? The "50% for construction" thing? Does that mean that I optimized my app well? I've been taught that the most time-consuming operations for web app are database queries. I optimized them, applied some caches, and now they are totally under control. And I actually have no clue on how to optimize class construction. Should I try to optimize these __construct() methods or leave them be?

+2  A: 

Is it the construct functionality or is it what construct is calling? Are you using a tool where you can see what those 50% are comprised of? Since most of that work could be calling other functions.

Ólafur Waage
I'm not sure what do you mean by "construct functionality". For example, my User class in `__construct()` method gets the instances of `Database` and `Config` classes, defines some default valuess and then tries to find out if current user is logged in registered user or some anonymous dude (1 query to db). All that stuff takes 0.02 seconds, or ~20% of all loading time.
n1313
What I mean is that is the construct function taking 50% of the process time. What are the individual components' time? How long is the stuff within the constructor taking.
Ólafur Waage
+3  A: 

I wouldn't worry too much; if you're not doing a lot of stuff to the data before you finish rendering your page the majority of the time running it will be spent in the constructors of your classes, this should more or less remain a constant penalty though. If you have a lot of logic running in the controller and your profiler is still telling you that it's spending a lot of time starting your app then you might want to consider optimising; remember that premature optimisation is a very bad thing, your time is better spent developing new features or fixing bugs unless the app's execution time is slow enough to be considered a bug.

Mathew Hall
Yes, this construction time seems to be more or less constant, yeah. A good point.
n1313
A: 

I once had to do some web services integrated in a Drupal site.

So I eventually did some profiling only to find out that the Drupal bootstrap was using 90% of the time and my code was taking the rest of 10%. Drupal is poorly engineered tho :), so it's loading lots and lots of stuff it doesn't really use.

I think it's up to you to decide in your case.

Prody