On 11 Jul 2003 14:35:43 -0700,
sc******@cableo ne.net (dSchwartz) wrote:
This may not be a big deal,
It isn't. :-0
but I'm just kinda curious. If I have a
page that submits a form to itself, when is it better to assign each
element in $_POST to a variable to use for the rest of the page?
My situation probably doesn't make a difference, but say I have about
10 form fields, and when the page is posted back to itself, i call
each of those form fields 2 times (1 for error checking, 1 for SQL
insert, or to pre-fill form).
So where does it become more efficient to only call $_POST once and
assign those to variables? Do you use the variables if they are
needed any more than one time?
If you're bothered about it, benchmark it.
<pre>
<?php
function getmicrotime(){
list($usec, $sec) = explode(" ",microtime ());
return ((float)$usec + (float)$sec);
}
$time_start = getmicrotime();
for ($i=0; $i<100000; $i++) {
$dummy = $_GET['x'];
}
$time_end = getmicrotime();
$time = $time_end - $time_start;
echo "Using \$_GET: $time seconds";
print "\n";
$value = $_GET['x'];
$time_start = getmicrotime();
for ($i=0; $i<100000; $i++) {
$dummy = $x;
}
$time_end = getmicrotime();
$time = $time_end - $time_start;
echo "Using variable: $time seconds";
?>
</pre>
On my creaky old P200 Linux server this comes out with:
Using $_GET: 1.2699910402298 seconds
Using variable: 1.0599220991135 seconds
That's if you use the value 100,000 times, this saves a grand total of just
over 0.2 seconds.
But what are the odds of you using a form variable that many times? And it's
almost certain you can get speed increases of many orders of magnitude greater
by improving your algorithms rather than shaving milliseconds off by avoiding
array access.
--
Andy Hassall (an**@andyh.co. uk) icq(5747695) (
http://www.andyh.co.uk)
Space: disk usage analysis tool (
http://www.andyhsoftware.co.uk/space)