Forum Moderators: coopster

Message Too Old, No Replies

Should I Whack Extra Code?

Impact on server performance

         

rogerd

3:57 pm on Jun 1, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I'm a real believer in flexible software, i.e., programs whose behavior can be altered by switches in a control panel as opposed to hacking the code. Ideally, this avoids most or all program modifications (and ensuing bugs), and makes future upgrading to new versions easy.

Now, though, I'm beginning to wonder. I've got a busy forum that will be switched to new software with PHP dynamic pages. The forum software is highly customizable via admin controls, and the code is loaded with sections that say, in essence, "IF displaystuff='yes' THEN <do some more logic and display a bunch of stuff">. There are dozens, if not hundreds, of lines of code associated with options I have no intention of using. I'm really tempted to delete big chunks of needless code rather than just relying on the admin switches.

I realize that without seeing the actual code, database calls, etc. it may be difficult to comment, but I'm looking for some general guidance as to whether I'm likely to speed things up by eliminating code chunks that will be mostly bypassed once a switch variable is checked. It's a busy site - I've had days pushing 1M pageviews, so even a little boost helps. OTOH, the ultimate HTML delivered is the same either way - any savings would be purely on the PHP processing side of things. Comments?

jatar_k

4:07 pm on Jun 1, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



everytime a decision needs to be made it takes time, not much but it still takes time. If you are looking at leaving it configured the way you have it I would just rip iit all out.

Needless conditional statements can take up noticable cycles and if the forum is very busy it can only help to rip out all of the junk you are never going to use.

It will also help customization since there will be no need to go through unused code.

trillianjedi

4:08 pm on Jun 1, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I would look at it from a processor percentage point of view. The saving would be in terms of clock cycles if the resultant HTML output is the same either way.

If, at your max of ~1m page views per day, your processor still has plenty of headroom and isn't really struggling, I don't think it would be worth you bothering.

If your server processor is already pushed, and you want to extend it's life without upgrading, then it might be worth considering. But you'd really have to shave off thousands of lines of code to make a noticable difference.

<edit>jatar_k just beat me to it, although I don't agree that (even lots) of unnecessary conditionals actually makes a "noticeable" difference in CPU useage overall.</edit>

TJ

jatar_k

4:12 pm on Jun 1, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



that's ok, I don't agree to put it off until your processor is pressed :)

not used == useless

therefore not needed

trillianjedi

4:15 pm on Jun 1, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



therefore not needed

Yes, I totally agree. When I wrote my post I was thinking (but didn't state) was that it comes down to time.

I'd rather live with a little useless bloat if I could spend my time on more productive things than getting rid of it.

That's I guess the balance that needs to be weighed up when considering whether or not to do it.

I don't think that you should remove something just because it isn't needed, if that time can be put to better use elsewhere.

If you have nothing else to do - then I agree and you should cut it out.

It is a nice feeling to do that too - quite rewarding. It's horrible when it goes wrong ("now where the hell did that bug spring from?!").

TJ

rogerd

4:29 pm on Jun 1, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I'm trying to complete most changes before going live, so evaluating the server load isn't an option at the moment. Personally, I have a feeling MySQL performance will be the bottleneck.

Most of the code I referred to looks like it can be eliminated without breaking other stuff, i.e., self-contained conditionals. I'd probably only get the biggest & most obvious ones; there are lots of little conditionals embedded in the display of individual bits of HTML that I probably wouldn't mess with under any circumstance.

httpwebwitch

4:29 pm on Jun 1, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The time wasted evaluating conditional statements is negligible. However PHP does need to *parse* all that code before it's delivered, because PHP likes to find syntax errors, even if they are contained in conditional statements that are always false.

Personally, I would hack out all the "dead wood". But the advice above is good; Check if your server is working too hard, and if it ain't broke...

Check the speed. You could put a microtimer on the page and test it before and after removing all the unused code. But in order to test the speed difference, you'd be removing the code, it's already done, and why bother testing the speed? Except that we here might like to hear about your results :-)

Consider the following 2 snippets. Does anyone know if there is a difference in performance between the two?


<?php if (false){?>
1 million lines of HTML
<?php }?>

or...


<?php
if(false){
// 1 million lines of code
}
?>

pete_m

4:32 pm on Jun 1, 2004 (gmt 0)

10+ Year Member



I think the time spent deleting the extra code could probably be better spent elsewhere.

First port of call should be the database, as it probably consumes the majority of the script execution time. Check that there are sufficient indexes, and that joins are optimised correctly. You may find that using a persistent connection brings better performance.

Are you using a php accelerator? If not, then this could speed things up as well.

After this, have a go at images. Have you set expiry dates on them, so that the browser caches them rather than requesting them on every page view? Are the file sizes as small as they can be without sacficing quality?

These are just off the top of my head... :)

corz

1:48 am on Jun 2, 2004 (gmt 0)

10+ Year Member



the cpu time will be marginal, the cpu time x 1M, still probably near unnoticable (a human expression). but unnecessary code is annoying, just sitting there, eating cycles. and what about 1.5M? or you get slashdotted? perhaps there's other useless code in there, too. maybe a rainy afternoon sometime soon, a wee test server, copy of ab, a timer..

the advice about the timer is top-notch. I put a microtime() starter in my header include, readout in my footer; everything is timed. this is more than trivial; when you're developing/customising anything where speed is a factor, knowing, quickly, that method A is 3% faster than method B is crucial information. especially 33 or so methods later.

Folks would be more sparing with their fancy regex if they had a timer on their pages!

;o)
(or

ps.. do check out ab (Apache Benchmark) should have been installed with apache.
pps.. yes httpwebwitch! a great difference! here's more fun for the performance pot, how about this one..


<?php
if (false) {
include('one.million.lines.of.code.php');
}
?>

ergophobe

6:28 pm on Jun 2, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month




check out ab (Apache Benchmark)

Which won't let you time individual sections, obviously, but will give a more accurate measure of total time for the page than will testing via microtime().


yes httpwebwitch! a great difference!

If it's a million lines, sure exiting PHP will always win. If it's a thousand lines, it depends a lot on what's in there. In my test of outputting a 2MB string 5x, the difference was 10% (which might be a lot, sure, but this is the extreme case). See
[webmasterworld.com...]