2

This is justa a performance question.

What is faster, access a local PHP variable or try to access to session variable?

5
  • 3
    -1: the .0000001ms a year you would have saved were lost merely by asking this question. Don't waste time on microoptimizations, and please don't be willing to shove local variables into the session just because you think it gives you an infinitesimal performance edge. Commented Sep 9, 2010 at 16:46
  • 5
    @juliet: He can't even ask the question out of curiosity? Commented Sep 9, 2010 at 16:49
  • @webbiedave Most of PHP developers believe that optimization stands for using "faster" operators. And such "out of curiosity" questions makes it even worst. Commented Sep 9, 2010 at 16:56
  • @Col. Shrapnel: I'd like to see a report on that specific stat. Otherwise, it's just empty chatter. Commented Sep 9, 2010 at 17:00
  • I wish this site was a bit more professional than enthusiast one... Commented Sep 9, 2010 at 17:08

4 Answers 4

7

I do not think that this makes any measurable difference. $_SESSION is filled by PHP before your script actually runs, so this is like accessing any other variable.

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

Comments

5

Superglobals will be slightly slower to access than non-superglobal variables. However, this difference will only be noticeable if you are doing millions of accesses in a script and, even then, such difference doesn't warrant change in your code.

$_SESSION['a'] = 1;
$arr['a'] = 1;

$start = 0; $end = 0;

// A
$start = microtime(true);
for ($a=0; $a<1000000; $a++) {
    $arr['a']++; 
}
$end = microtime(true);
echo $end - $start . "<br />\n";

// B
$start = microtime(true);
for ($b=0; $b<1000000; $b++) {  
    $_SESSION['a']++;   
}
$end = microtime(true);
echo $end - $start . "<br />\n";

/* Outputs: 
0.27223491668701
0.40177798271179

0.27622604370117
0.37337398529053

0.3008668422699
0.39706206321716

0.27507615089417
0.40228199958801

0.27182102203369
0.40200400352478
*/

1 Comment

Too bad there's no practical way to bench the session open/close overhead. Unserializing a session might be a bit more expensive than simply loading a .php file with a bunch of variable assignments in it.
0

It depends, are you talking about setting the $_SESSION variable to a local variable for use throughout the file or simple talking about the inherent differences between the two types of variables?

One is declared by you and another is core functionality. It will always be just a smidge slower to set the $_SESSION variable to a local variable, but the differenceenter code here is negligible compared to the ease of reading and re-using.

Comments

0

There is nothing performance-related in this question.
The only those performance questions are real ones, which have a profiling report in it.
Otherwise it's just empty chatter.

Actually such a difference will never be a bottleneck.
And there is no difference at all.

1 Comment

You can just answer "NO, there is no difference". Thank you for your "common sense"

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.