Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
500 views
in Technique[技术] by (71.8m points)

session - PHP ::: Speed Test ::: $_SESSION vs. $variable

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...