Jump to content

How can I prevent slow-loading pages?


kaloo

Recommended Posts

Hello
First, I want to thank all of you who keep this site at such a high level. I have solved a lot of my problems by reading articles posted here. Second, I want to apologize if any of my questions already have been answered earlier. It is not always that easy finding the answers one is looking for.

- Even though I have a rather small website, I want it to load at a minimum amount of time. Therefore I wondered:

1. When parsing a document before sending it to the client, does the server only read what is necessary or will bigger files slow it down. Let's say I have a index.php which is on 20kb, but the output is only 5kb. If I'm able to reduce the file's size, would that have any impact at all on the loading time? I guess this is basic, but I would still like to know it for sure.
2. How time consuming is the include function? Should I try using it as little as possible or doesn't it matter ?
3. Let's say I have:
[code]
<?php
if ($name == "kaloo") {
// execute lots of code
}
?>
[/code]
If that code is quite big, should it rather be an own file which is included?
4. What else should I be aware of to minimize loading time?

Any thoughts on this will be highly appreciated, as would any links to sites dealing with this matter (php/non-php).
Link to comment
Share on other sites

Firstly - thankyou for the way you worded your query, great to see respect like that for other users. :)

Secondly, I'm a middle-competence coder at best, and often need to use these forums myselves to ask the real coders for advice and help -- so I don't know how qualified I am to answer your questions, but I'll give it a shot!


What I do know is that it's DEFINITELY not just the output size that's gonna have an impact on loading speeds. This isn't even the main element. Output size may matter on some level, but computing time is what you need to worry about.

I'm pretty sure you can have anything in if conditions, without it effecting the runspeed unless the condition is met. (The check itself presumably takes some time, but we're talking seriously nothing.)

Some ways of doing the same function are more efficient, and optimising code aims to get memory usage etc. down as well as page load time. To be honest, you shouldn't worry hugely - just try and make code clean. For what you've described, I doubt optimising is of vast importance: if it's slow, it's probably your server.

Having said that, try to do things in the cleanest order, try to avoid unnecessary tasks (eg. always retrieving some info that you only need occasionally... check first, etc.).

But seriously dude, for what you're describing the difference between clumsy and streamlined code should be very close on impossible to notice... slow is probably a server issue instead. If you've bottlenecks slowing your script, you should really be able to identify what they are -- a loop repeating something 600 times over is gonna take some time - looking up pages takes time, etc.

One thing you can do is output the time it takes your scripts to run and make tweaks, seeing which of two ways are faster.

Secondly, computing time and script size are very, very different concepts. I could write a 5 line loop that takes hours to load, and a 2,000 line script that takes seconds.
Link to comment
Share on other sites

Thank you for your reply. I am not really having a great problem with this yet, as my website is still very small and with little scripting. However I thought it would be good to know something about this when I continue the "voyage into the world of php". It is good to know that I can have a site that both performs well and is easy to maintain. I might just look over my coding to see if I can clean it up a bit though.
Link to comment
Share on other sites

What you should really be concerning yourself with is the algorithms you use to accomplish your tasks.

Pretend you're a mechanic working on the four wheels of a car.  Let's say you need to remove each wheel of the car, do some stuff, and then put the wheel back on.

Consider the following procedures:
Get out tools to remove wheel #1
Remove wheel #1
Put away tools to remove wheel #1
 Get out tools to do procedure
 Do procedure to wheel #1
 Put away tools to do procedure
Get out tools to replace wheel #1
Replace Wheel #1
Put away tools to replace wheel #1

Move onto wheel #2 and repeat the whole process.

Or are you going to do it like this:
Get out the tools to remove a wheel
Remove all four wheels
Put away the tools to remove a wheel
 Get out the tools to do procedure
 Do procedure to all four wheels
 Put away tools to do procedure
Get out tools to replace a wheel
Replace all four wheels
Put away tools to replace a wheel

The choice is fairly obvious.  The second process, or algorithm, is more efficient.  It involves a lot less time walking back and forth in the garage getting out and putting away the same tools over and over again.

It seems obvious in an example such as that, but a lot of times when looking at another person's code, you'll see that they're doing things as inefficiently as the first mechanic.

If you really want to create fast loading pages, try to develop efficient ways to work with your data.

It really doesn't matter how many includes, function calls, variable assignments or reads, or whether or not you use single or double quotes.  You can streamline all of that as much as possible and you'll shave maybe a few milliseconds off your script's execution time.

The stuff you really want to watch out for is repeated loops over the same data or horribly inefficient MySQL queries.  It doesn't matter if your page is processed in less than a second if the 3 MySQL queries on it take a total of 30 seconds to execute.

The key thing to remember is to use efficient algorithms but to maintain the readability of your code.  It's not worth optimizing the bejesus out of your code to gain a millisecond of execution time if you can't easily tell what the code is doing 6 months down the road.

You should try and apply that concept to 99% of what you write.  After all is said and done, if you have a piece of your site that is performing much too poorly, you can go back and optimize just that portion of your code.

As a final comment, if you have an if body that is really big, consider putting it into another function and calling just that function rather than creating another .php file to include().
Link to comment
Share on other sites

Thank you for your reply roopurt18. I aggree that effecient algorithms are more important. I suppose I just needed someone else to make it clear to me. I will certainly look over my code again. If you could amplify the last comment, I did not quite understand it. Thank you
Link to comment
Share on other sites

If you have:

if(true){
  // 110 statements
}

You chould change it to
if(true){
  DoSomething();
}

function DoSomething(){
  // 110 statements
}

instead of
if(true){
  include('110statements.php');
}

I find having include all over the place often sends you on a wild goose chase as to what file is crashing your script.  And when you're passing data back and forth between files like this it can get really messy.  I come from a C / C++ background where this type of stuff just wasn't used, so I've never been comfortable with it.  The current project I've inherited is full of it and it drives me crazy.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.