Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Senior Coder NancyJ's Avatar
    Join Date
    Feb 2005
    Location
    Bradford, UK
    Posts
    3,169
    Thanks
    19
    Thanked 65 Times in 64 Posts

    building lists taking way too long.

    This is just a tiny simple bit of code but it takes way longer to run that I think it should. I'm going through our website and trying to cut down the load time (currently 0.5s to generate the page)

    PHP Code:
    $start microtime(true);
            for(
    $i=1;$i<=31;$i++)
            {
                
    $options .= "<option value = \"$i\">$i</option>";
            }
            echo 
    $options;
    echo 
    "Echo day ".(microtime(true)-$start)."<br />"
    this code takes 0.07s to run. For what it does, that is redicilous. Interestingly, if I keep refreshing the page, eventually it goes down to about 0.0001, but why? Everything else seems to stay around the same time.

  • #2
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,040
    Thanks
    10
    Thanked 92 Times in 90 Posts
    I doubt that code is the main issue .. on my local testing debian box (ony 800 Duron!) I get 0.0006ish consistently

    (note that `++$i` and `$options .= '<option value = "'.$i.'">'.$i.'</option>';` takes that down to 0.00038ish )

    .. but that box is doing little else .. perhaps your server is just busy In general ?
    Small optimizations like the above can make a difference but only the tiniest... database connections, database structure, regex etc will have far bigger performance ramifications than this sort of stuff.
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #3
    fci
    fci is offline
    Senior Coder
    Join Date
    Aug 2004
    Location
    Twin Cities
    Posts
    1,345
    Thanks
    0
    Thanked 0 Times in 0 Posts
    how about caching html? take a look at ob_get_contents for some starting ideas
    http://us3.php.net/ob_get_contents

  • #4
    Senior Coder NancyJ's Avatar
    Join Date
    Feb 2005
    Location
    Bradford, UK
    Posts
    3,169
    Thanks
    19
    Thanked 65 Times in 64 Posts
    Quote Originally Posted by firepages
    I doubt that code is the main issue .. on my local testing debian box (ony 800 Duron!) I get 0.0006ish consistently

    (note that `++$i` and `$options .= '<option value = "'.$i.'">'.$i.'</option>';` takes that down to 0.00038ish )

    .. but that box is doing little else .. perhaps your server is just busy In general ?
    Small optimizations like the above can make a difference but only the tiniest... database connections, database structure, regex etc will have far bigger performance ramifications than this sort of stuff.
    I'm timing everything, and there are only 3 other things taking longer than this one - I picked this one first because it is taking longer than it should. The other lists (for month and year) are normal times. Taking the same code and running it in a page on its own it runs at around 0.0004, but within the page its running on the first couple of runs its really slow.

  • #5
    Senior Coder NancyJ's Avatar
    Join Date
    Feb 2005
    Location
    Bradford, UK
    Posts
    3,169
    Thanks
    19
    Thanked 65 Times in 64 Posts
    Quote Originally Posted by firepages
    (note that `++$i` and `$options .= '<option value = "'.$i.'">'.$i.'</option>';` takes that down to 0.00038ish )
    ..Not sure I know exactly what you mean there where are you putting ++$i?
    Can you post your whole example as it doesnt work for me.

  • #6
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,040
    Thanks
    10
    Thanked 92 Times in 90 Posts
    ++$i is probably insignificant (though internally $i++ is aparantly converted to ++$i before use) , the biggest gain would be the use of single quotes...
    PHP Code:
    <?
    $start 
    microtime(true);
            for(
    $i=1;$i<=32;++$i)
            {
                
    $options .= '<option value = "'.$i.'">'.$i.'</option>';
            }
            echo 
    '<select>'.$options.'</select>';
    echo 
    "Echo day ".(microtime(true)-$start)."<br />";  
    ?>
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #7
    Senior Coder NancyJ's Avatar
    Join Date
    Feb 2005
    Location
    Bradford, UK
    Posts
    3,169
    Thanks
    19
    Thanked 65 Times in 64 Posts
    why would using single quotes cause speed increases? The difference in speed is amazing. I've always used double quotes because they're easier but they do make a significant difference.

  • #8
    Senior Coder
    Join Date
    Sep 2005
    Posts
    1,791
    Thanks
    5
    Thanked 36 Times in 35 Posts
    duoble-quoted strings have to be searched in their entirety for variables to be parsed, sinqle-quoted ones don't and so can be effectively stepped over.


  •  

    Posting Permissions

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