Monday, May 7, 2012

Does initiating a large class on a page slow it down?


I am in the process of writing a class that will probably end up being around 3000 lines of code.



What I would like to know is very simple, will initiating this class at the top of each page slow down the runtime of the page, even though only one/two of the objects methods will be used? Is it going to put a lot more strain on my server if it is being accessed several thousand times a day?



If so, should I be looking at creating extensions to handle each method instead of having the whole class in one file?



EDITED



Firstly, just to correct KingCrunch and Kenaniah, this class is for my API, which resultantly means that it holds a lot of functions for retrieving data to be displayed on the website and our iPhone Application, along with our entire Facebook Application. So 3000 lines is pretty damn small given the size and capabilities of our website, not to mention that over 700 of these lines are comments. So I can assure you there is no design flaw, although there may be a structural flaw, which is why I am asking this question...



The construct function simply sets the default values to the defined variables, nothing more.



I have completely rewritten this file from scratch so there is no old code and I am pretty sure the methods within the class are as efficient as they can possibly be.



I have been monitoring my server usage etc, as well as simulating high volumes of traffic using the apache ab tool and although my memory usage shoots up, it does seem to be okay.


Source: Tips4all

3 comments:

  1. will initiating this class at the top of each page slow down the runtime of the page


    Will it add to the runtime? Yes. Of course. Nothing is free. Every line of code parsed has some small overhead (however you can get rid of most of this cost with a opcode cache like APC). However, we're talking sub-millisecond overhead, probably. The only way to be sure is to profile it yourself.


    Is it going to put a lot more strain on my server if it is being accessed several thousand times a day?


    From personal experience, no. But again, profile and measure yourself. You should be monitoring basic performance metrics on your server (CPU usage, load average, etc.). Deploy your change, and watch your metrics.

    ReplyDelete
  2. No, instantiating a class that is made up of lots of LOC, does not automatically make it slow.

    That is, unless you do something in the constructor, but then it depends on what you are doing there, and not how big the class is.

    ReplyDelete
  3. no, actually it's faster than splitting it in multiple files.

    the only problem is that often result in a big block of code and modifications are harder to do.

    EDIT: it will be faster if all the lines are usefull. if you have a lot of old code you might consider a clean-up

    ReplyDelete