简体   繁体   English

PHP :::速度测试::: $ _SESSION与$ variable

[英]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. 我声明$ _SESSION vars,以便在验证失败时,不必为了给用户带来便利就将通过的数据重写。

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? 大批量生产时,一种产品的性能要比另一种产品更快吗?

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花了0.079180002212524微秒$ _SESSION ['hey']花了0.096824884414673微秒$ hey_array ['hey']花了0.091028928756714微秒

$hey took 0.080883026123047 microseconds $_SESSION['hey'] took 0.095050096511841 microseconds $hey_array['hey'] took 0.091977834701538 microseconds $ hey花了0.080883026123047微秒$ _SESSION ['hey']花了0.095050096511841微秒$ hey_array ['hey']花了0.091977834701538微秒

$hey took 0.081928968429565 microseconds $_SESSION['hey'] took 0.097215890884399 microseconds $hey_array['hey'] took 0.092087030410767 microseconds $ hey花了0.081928968429565微秒$ _SESSION ['hey']花了0.097215890884399微秒$ hey_array ['hey']花了0.092087030410767微秒

$hey took 0.081655979156494 microseconds $_SESSION['hey'] took 0.098057985305786 microseconds $hey_array['hey'] took 0.09247899055481 microseconds $ hey花了0.081655979156494微秒$ _SESSION ['hey']花了0.098057985305786微秒$ hey_array ['hey']花了0.09247899055481微秒

$hey took 0.081120014190674 microseconds $_SESSION['hey'] took 0.096808910369873 microseconds $hey_array['hey'] took 0.092255115509033 microseconds $ hey花了0.081120014190674微秒$ _SESSION ['hey']花了0.096808910369873微秒$ hey_array ['hey']花了0.092255115509033微秒

$hey took 0.081827878952026 microseconds $_SESSION['hey'] took 0.096134901046753 microseconds $hey_array['hey'] took 0.092247009277344 microseconds $ hey花了0.081827878952026微秒$ _SESSION ['hey']花了0.096134901046753微秒$ hey_array ['hey']花了0.092247009277344微秒

$hey took 0.081613063812256 microseconds $_SESSION['hey'] took 0.096814870834351 microseconds $hey_array['hey'] took 0.090691804885864 microseconds $ hey花了0.081613063812256微秒$ _SESSION ['hey']花了0.096814870834351微秒$ hey_array ['hey']花了0.090691804885864微秒

So, in 100,000 loops, we're talking about .01 MICROSECONDS. 因此,在100,000个循环中,我们谈论的是.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). 它是一个超全局的事实并不会影响它(但是,数组的长度会影响它的长度,如果您的SESSION数组很大,您将开始看到微小的速度差异(但同样,我们谈论的是百分之一秒的百分之一)。

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. 我没有对此进行任何基准测试,但是我强烈怀疑$ _SESSION [] 总是比普通变量写入慢一些,因为它由文件系统或数据库支持。 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. 即使所有$ _SESSION []存储都已缓存,使用该存储所涉及的CPU和内存活动仍比使用仅本地存储的变量要多。 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. 如果您想提高性能,请考虑在JavaScript中复制一些输入验证。 (Don't REPLACE server-side validation, as JS is not universal; just consider ways to avoid requiring it if possible.) (不要替换服务器端验证,因为JS不是通用的;请尽可能考虑避免使用它的方法。)

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. 它是$_SESSION数组的事实没有任何性能。 It's the associative array lookup that takes slightly longer than accessing a variable. 它是关联数组查找所花的时间比访问变量所需的时间稍长。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM