Does initiating a large class on a page slow it down?
Asked Answered
S

3

9

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.

Sacral answered 18/1, 2012 at 18:25 Comment(8)
a 3000 LOCs-class sounds more like a design flaw, than a performance problem ...Interchange
In short, yes it will slow it down because it will have to initialize that class. It probably isn't as bad as you think it is though. Sounds like you might want to look at this related question: #1424882Burr
Short answer: Yes. Long answer: There are an infinite set of factors that slow down a page. What you lose in 3000 lines of code could be made up elsewhere.Sometime
what does the class do? and why instantiate it on each page?Amongst
3000 lines of code? You need to refactor, because you're definitely doing something wrong.Coimbra
Why dont you break it down in smaller pieces? take out the common functions & create a new class.. inheritance will help if they are dependent. Since you are using only couple methods, it will be better if you take a good look at it and redesign your classRepression
Even if you try to explain: 2300 LOCs for a single class is too much.Interchange
Well this class is over 5000 lines long, are you telling me you could achieve the same in less than 1000? Upload ClassSacral
L
6

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.

Laryngitis answered 18/1, 2012 at 18:39 Comment(3)
for the first part, it depend, if he needs the 3k lines, and consider splitting it in multiple classes, he will end up loading them all one by one, wich will slower his performances.Handwork
Yep, including ten 300 line files is (a bit) slower than including one 3000 line file. Again, the actual difference in wall-time is probably pretty trivial.Laryngitis
But with autoloading, it may not be necessary to load every "subclass" for every request, and this is also a good justification for using APCJessamine
L
1

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.

Lapboard answered 18/1, 2012 at 18:37 Comment(0)
H
1

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

Handwork answered 18/1, 2012 at 18:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.