PHP ::: 速度测试 ::: $_SESSION vs. $variable [英] PHP ::: Speed Test ::: $_SESSION vs. $variable

查看:29
本文介绍了PHP ::: 速度测试 ::: $_SESSION vs. $variable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个输入量相当大的表单,而且流量也很高.我声明 $_SESSION 变量,以便在验证失败时通过的数据不必为了用户方便而重写.

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.

我的问题是,从那时起,在使用 a 的方法中是否会更快:

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

$hey; //regular variable

或者:

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

性能上有区别吗?在高容量情况下,一个的执行速度是否比另一个快?

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

推荐答案

我没有做过任何基准测试,但我强烈怀疑 $_SESSION[] 将总是比写入的普通变量,因为它由文件系统或数据库支持.即使所有 $_SESSION[] 存储都被缓存,与使用仅在本地保存的变量相比,使用它仍然会涉及更多的 CPU 和内存活动.对于读取而言,仍然可能存在微小差异,但与其他因素相比,可以忽略不计.

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.

如果您希望提高性能,请考虑在 JavaScript 中复制一些输入验证.(不要替换服务器端验证,因为 JS 不是通用的;如果可能,只需考虑避免需要它的方法.)

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

这篇关于PHP ::: 速度测试 ::: $_SESSION vs. $variable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆