2

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 rewritten out of convience to the user.

After I validate my form input like this:

$hey = htmlspecialchars($_POST['hey']);

if( correct_value($hey) == TRUE ) {
    $_SESSION['hey'] = $hey; 
}

I now have two variables with the same value in the same method where I update the db etc.

My question, is it faster from then on within the method to work with a:

$hey; //regular variable

Or a:

$_SESSION['hey']; //session variable

Is there a difference in performance? At high volumes does one perform faster than the other?

3
  • 6
    You probably have a lot of other, less complex optimization to do before you get to hash lookup vs. normal variable lookup. Commented Jan 27, 2012 at 3:54
  • 2
    $_SESSION variables may be a little more memory-intensive because it appears to be a "superglobal" variable. Even so, $_SESSION variables can be convenient to use. I guess it ultimately depends on the needs of your script/program. Reference: php.net/manual/en/reserved.variables.session.php Commented Jan 27, 2012 at 3:59
  • I would not bother myself with this kind of optimization. It's penny wise and pound foolish Commented Jan 27, 2012 at 4:14

3 Answers 3

6

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

Sign up to request clarification or add additional context in comments.

Comments

2

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

2 Comments

@Graham Not sure you're right about the filesystem factor. The file writing only happens once when the variable is serialized at the end of script execution. While using sessions does add overhead to the script execution, it doesn't impact variable access performance.
I wasn't aware that $_SESSION was only serialized and written when the script finishes. For the sake of argument, I suppose the OP's question "is it faster from then on" could be considered specifically within the context of the script or holistically with the server. If in general you're making changes to $_SESSION variables needlessly, you are generating more filesystem writes than you need, which slows down the whole server. But of course, this is spurious; I agree that the impact would be negligible.
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.