Results 1 to 6 of 6

Thread: loading speed

  1. #1
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,285
    Thanks
    97
    Thanked 104 Times in 102 Posts

    Default loading speed

    Lets say we have two different files or web pages:

    PHP Code:
    <?php
    $q
    =3;
    if (
    $q==3) echo "$q";
    ?>
    PHP Code:
    <?php
    $q
    =3
    if ($q==2) {1-3 MB worth of instructions that won't be executed, because the $q =3 not 2;}
    if ($q==3) echo "$q";
    ?>
    could there be a significant difference in loading speeds?
    Last edited by james438; 01-24-2010 at 06:38 AM.
    To choose the lesser of two evils is still to choose evil. My personal site

  2. #2
    Join Date
    Sep 2008
    Location
    Bristol - UK
    Posts
    842
    Thanks
    32
    Thanked 132 Times in 131 Posts

    Default

    But if that's what you want your code to do, there's really nothing you can do to minimize the speed of execution. And on something like that, it would be absolutely miniscule and completely unnoticeable if you loaded the page.

    It all depends on the scenario, where for example you could cut down the code to make a page load faster, but the more things you want the script to do, the more memory it will inevitably take up.

    Edit: I realise now what you meant:
    PHP gets to the if statement, looks at it. Says $q does not equal 2... and so skips ahead to the next part, so it's not like it has to go through the instructions contained in the $q == 2 statement.

    Short answer: Not a significant difference.

  3. #3
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,285
    Thanks
    97
    Thanked 104 Times in 102 Posts

    Default

    Sorry, I am not understanding you here. If the page will not be executing 1-3 MB worth of instructions because $q does not = 3 will it take a long time for the server to "look over" all of that code that won't be executed? I could have easily said 10 - 30 MB worth of data that will not be executed as well. Is the unexecuted code being processed in some way or is it being skipped over as if it wasn't there?
    To choose the lesser of two evils is still to choose evil. My personal site

  4. #4
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    There is more text in one. That will make it load slower. It should not matter in general, but if it is 3mb, you might run into problems with memory allocation and processing overload. And, yes, if it is that much, it may make it run slower.

    However, here is how PHP works:
    1. The entire file is scanned and parsed: high level errors (like unexpected symbols) appear here-- it doesn't know how to start processing.
    2. Then it starts at the beginning of the file and actually processes each line, doing the calculations, checking ifs, etc.

    Therefore, PHP will run through ALL of your code first, then the second time it will only do what makes sense: it will check the if statements.

    The FIRST time, all of that text will slow it down. The SECOND time, it will only read if ($q==2) { and skip all of THIS. }

    If there was 3mb of stuff NOT inside an if (at the highest level), then that would slow it down the second time as well.

    The logical way to approach this is to move the shorter things first, but since, again, the bigger code only slows it down during the first, complete, run, it won't make a difference if the contents are being skipped anyway.

    Because you have two different operations (I think), and because one is so large, finding a way to split it would make sense.

    All actual commands (rather than structure) are executed AFTER all of the parsing, including any include/require commands.
    Because of this, you can do a much simpler method:
    if (a) { include('3mbfilehere.php'); }
    if (b) { simple command }

    Then just put your 3mb of php into an external file and that will only be loaded AND only be parsed AFTER the include command is EXECUTED.
    So if the statement "a" is false, then that file will never be parsed or slow things down in any way.


    Your code above is somewhat vague, though, and I'm not sure what you're trying to do exactly, so if you can post the real code or explain the real problem I might be able to give more specific advice.



    EDIT: My advice in general is to never have 3mb of php code. That is a LOT of text (3,000,000 characters, so let's say 30 characters per line: that's 100,000 lines of code!!!). PHP scripts over 100kb are rare.
    Once you end up with that much text, it is ALWAYS a good idea to find some way to split it up. Having everything parsed on one page is a problem (memory, processing, speed), but also very difficult to keep track of for editing. Keep different sets of operations in different places and include them as needed.
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  5. #5
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,285
    Thanks
    97
    Thanked 104 Times in 102 Posts

    Default

    That clears things up a lot for me. I was vague in my code, because I don't really have that problem, I was just wondering how PHP looks at code. I also wanted to know in case I wanted to create some very different pages depending on the browser or the user. I was not thinking of the include function, because I figured the server would process the code regardless. The reason I said 1 to 3 MB or even 10 to 30 MB of unprocessed code was because I wanted to look at some possible extremes in PHP file sizes.

    When you say that PHP parses the code the first time and processes each line one at a time the second time (thus skipping the nebulous data I mentioned earlier) I assume you mean that this happens for each person who visits the page.

    Either way it looks like includes are still the way to go here. After looking at my web pages the largest php file I have is about 5kb and the largest text file (with a little php) is 36 kb.
    To choose the lesser of two evils is still to choose evil. My personal site

  6. #6
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Anything over 100kb is going to be a very long php file. It's not impossible-- sometimes php might even reach 1mb if it is VERY complex, but most php coders who would actually get that much code written would know to split it-- otherwise you'll end up with big issues just coding it, not to mention processing later.


    I'm not actually sure about whether it is reprocessed for each user. I know that this happens when you load a page for the first time (after uploading it), but it might actually store a "parsed" value somewhere. But still it would need to run through that much text (even just to skip it, like comments), and you'd be better off splitting it to work with.
    I think it's possible (maybe with server settings) that you would run into it parsing for every user, though not sure. Best to avoid it.
    That's a good question, though. Not sure where to look it up.
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •