...

View Full Version : building lists taking way too long.



NancyJ
06-20-2006, 04:38 PM
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)



$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.

firepages
06-20-2006, 04:59 PM
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.

fci
06-20-2006, 05:05 PM
how about caching html? take a look at ob_get_contents for some starting ideas
http://us3.php.net/ob_get_contents

NancyJ
06-20-2006, 05:27 PM
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.

NancyJ
06-20-2006, 05:34 PM
(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.

firepages
06-20-2006, 05:39 PM
++$i is probably insignificant (though internally $i++ is aparantly converted to ++$i before use) , the biggest gain would be the use of single quotes...


<?
$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 />";
?>

NancyJ
06-20-2006, 05:41 PM
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.

GJay
06-20-2006, 07:12 PM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum