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

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.