kaloo Posted November 1, 2006 Share Posted November 1, 2006 HelloFirst, 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]<?phpif ($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). Quote Link to comment Share on other sites More sharing options...
Anidazen Posted November 1, 2006 Share Posted November 1, 2006 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. Quote Link to comment Share on other sites More sharing options...
kaloo Posted November 1, 2006 Author Share Posted November 1, 2006 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. Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted November 1, 2006 Share Posted November 1, 2006 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 #1Remove wheel #1Put away tools to remove wheel #1 Get out tools to do procedure Do procedure to wheel #1 Put away tools to do procedureGet out tools to replace wheel #1Replace Wheel #1Put away tools to replace wheel #1Move onto wheel #2 and repeat the whole process.Or are you going to do it like this:Get out the tools to remove a wheelRemove all four wheelsPut 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 procedureGet out tools to replace a wheelReplace all four wheelsPut away tools to replace a wheelThe 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(). Quote Link to comment Share on other sites More sharing options...
kaloo Posted November 1, 2006 Author Share Posted November 1, 2006 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 Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted November 1, 2006 Share Posted November 1, 2006 If you have:if(true){ // 110 statements}You chould change it toif(true){ DoSomething();}function DoSomething(){ // 110 statements}instead ofif(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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.