I have a form with a fairly large amount of input that will also be high traffic. I declare $_SESSION vars so that on validation fails data that passes doesnt have to be rew
There is a marginal difference in performance. When I say marginal, I mean really, really small. The fact that it's the $_SESSION
array doesn't have any bearing performance. It's the associative array lookup that takes slightly longer than accessing a variable.
I haven't done any benchmarks on this, but I strongly suspect that $_SESSION[] will always be a little slower than normal variables for writes, because it's backed by the filesystem or database. Even if all $_SESSION[] storage is cached, there will still be more CPU and memory activity involved in using it than in using variables that are only held locally. For reads, there will still likely be a small difference, but it'll be negligible compared to other factors.
So the comment above is right on the money. You have other areas of optimization that are more important than this one.
If you're looking to improve performance, consider duplicating some of your input validation in JavaScript. (Don't REPLACE server-side validation, as JS is not universal; just consider ways to avoid requiring it if possible.)
I'm going to second the comments here, if you're going to optimize you're not going to get much improvement by eliminating duplicate variables (though if you have huge variables you want to eliminate duplication to preserve memory - not speed). However, for the sake of demonstration, I set up this benchmark to test:
session_start();
$hey = "THIS IS A TEST OF ACCESS SPEEDS"; //our variable
$_SESSION['hey'] = $hey; //out session variable
$hey_array = array('a'=>'random','b'=>'random','c'=>'random'); //another random array
$hey_array['hey'] = $hey;
function access_the_variable($var){
$waste_some_time = substr($var,0,10); //this could be anything
}
//GO!
$start = microtime(true);
for($i=0;$i<100000;$i++){
access_the_variable($hey);
}
$end = microtime(true);
echo "\$hey took ".($end-$start)." microseconds<br />";
$start = microtime(true);
for($i=0;$i<100000;$i++){
access_the_variable($_SESSION['hey']);
}
$end = microtime(true);
echo "\$_SESSION['hey'] took ".($end-$start)." microseconds<br />";
$start = microtime(true);
for($i=0;$i<100000;$i++){
access_the_variable($hey_array['hey']);
}
$end = microtime(true);
echo "\$hey_array['hey'] took ".($end-$start)." microseconds<br /><br />";
The results of several runs:
$hey took 0.079180002212524 microseconds $_SESSION['hey'] took 0.096824884414673 microseconds $hey_array['hey'] took 0.091028928756714 microseconds
$hey took 0.080883026123047 microseconds $_SESSION['hey'] took 0.095050096511841 microseconds $hey_array['hey'] took 0.091977834701538 microseconds
$hey took 0.081928968429565 microseconds $_SESSION['hey'] took 0.097215890884399 microseconds $hey_array['hey'] took 0.092087030410767 microseconds
$hey took 0.081655979156494 microseconds $_SESSION['hey'] took 0.098057985305786 microseconds $hey_array['hey'] took 0.09247899055481 microseconds
$hey took 0.081120014190674 microseconds $_SESSION['hey'] took 0.096808910369873 microseconds $hey_array['hey'] took 0.092255115509033 microseconds
$hey took 0.081827878952026 microseconds $_SESSION['hey'] took 0.096134901046753 microseconds $hey_array['hey'] took 0.092247009277344 microseconds
$hey took 0.081613063812256 microseconds $_SESSION['hey'] took 0.096814870834351 microseconds $hey_array['hey'] took 0.090691804885864 microseconds
So, in 100,000 loops, we're talking about .01 MICROSECONDS.
However, it's worth noting that the speed difference is almost entirely attributable to needing to access an associative array. The fact that it's a superglobal doesn't affect it (the length of the array does, however, and you will start seeing tiny speed differences if your SESSION array gets huge (but again, we're talking hundreths of millionths of seconds).