Skip to main content

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

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

Post a Comment

Popular posts from this blog

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex