avi*_*viv 13 php oop performance
在PHP5中,我可以向类声明一个const值:
class config
{
const mailserver = 'mx.google.com';
}
Run Code Online (Sandbox Code Playgroud)
但我也可以宣布公开静态:
class config
{
public static $mailserver = 'mx.google.com';
}
Run Code Online (Sandbox Code Playgroud)
如果是配置文件,我将在以后使用,例如:
imap_connect(config::$mailserver ...
imap_connect(config::mailserver ...
Run Code Online (Sandbox Code Playgroud)
您认为哪个选项更适合使用?(更快,更少的内存负载等..)
谢谢.
小智 10
使用函数返回全局
0.0096,0.0053,0.0056,0.0054,0.0072,0.0063,0.006,0.0054,0.0054,0.0055,0.005,0.057,0,053,0.0049,0.064,0.0054,0.0053,0.0053,0.0061,0.0059,0.0076,config1
使用get instance normal class
0.0101,0.0089,0.0105,0.0088,0.0107,0.0083,0.0094,0.0081,0.0106,0.0093,0.0098,0.0092,0.009,0.0087,0.0087,0.0093,0.0095,0.0101,0.0086,0.0088,0.0082,config2
使用静态变量
0.0029,0.003,0.003,0.0029,0.0029,0.0029,0.003,0.0029,0.003,0.0031,0.0032,0.0031,0,029,0.0029,0.0029,0.0029,0.0031,0.0029,0.0029,0.0029,0.0029,config3
使用const var 0.0033,0.0031,0.0031,0.0031,0.0031,0.0031,0.0032,0.0031,0.0031,0.0031,0.0031,0.0034,0.0031,0.0031,0.0033,0.0031,0.0037,0.0031,0.0031,0.0032,0.0031,config4
function getTime() {
$timer = explode( ' ', microtime() );
$timer = $timer[1] + $timer[0];
return $timer;
}
$config["time"] = "time";
class testconfig2
{
public $time = "time";
static $instance;
static function getInstance()
{
if(!isset(self::$instance))
self::$instance = new testconfig2();
return self::$instance;
}
}
class testconfig3
{
static $time = "time";
}
class testconfig4
{
const time = "time";
}
function getConfig1()
{
global $config;
return $config;
}
$burncount = 2000;
$bcount = 22;
for($lcnt =1;$lcnt < $bcount;$lcnt++){
$start = getTime();
for($i=1;$i< $burncount;$i++)
{
$gs=getConfig1();
$t = $gs["time"];
}
$end = getTime();
echo round($end - $start,4).', ';
}
echo ' config1<br/>';
for($lcnt =1;$lcnt < $bcount;$lcnt++){
$start = getTime();
for($i=1;$i< $burncount;$i++)
{
$gs=testconfig2::getInstance();
$t = $gs->time;
}
$end = getTime();
echo round($end - $start,4).', ';
}
echo ' config2<br/>';
for($lcnt =1;$lcnt < $bcount;$lcnt++){
$start = getTime();
for($i=1;$i< $burncount;$i++)
{
$gs=testconfig3::$time;
$t = $gs;
}
$end = getTime();
echo round($end - $start,4).', ';
}
echo ' config3<br/>';
for($lcnt =1;$lcnt < $bcount;$lcnt++){
$start = getTime();
for($i=1;$i< $burncount;$i++)
{
$gs=testconfig4::time;
$t = $gs;
}
$end = getTime();
echo round($end - $start,4).', ';
}
echo ' config4<br/>';
?>
Run Code Online (Sandbox Code Playgroud)