在对类执行100w次循环后, 常量最快,变量其次,静态变量消耗时间最高
其中:
常量消耗:101.1739毫秒
变量消耗:2039.7689毫秒
静态变量消耗:4084.8911毫秒
class Timer_profiler {
public static $begin_timer;
public static $finish_timer;
public static $timer_html;
/**
* 计算时间差
* @return type
*/
public static function getRecordTimer() {
return (self::getFinishTimer() - self::getBeginTimer()) * 1000;
}
/**
* 生成输出HTML
* @param type $message
* @return type
*/
public static function recordHtml($message = '') {
self::setFinishTimer();
return "<p>{$message}耗时: " . self::getRecordTimer() . " 毫秒</p>";
}
/**
* 设置开始时间
*/
public static function setBeginTimer() {
self::$begin_timer = self::getTime();
}
/**
* 设置结束时间
*/
public static function setFinishTimer() {
self::$finish_timer = self::getTime();
}
/**
* 获取开始时间
* @return type
*/
public static function getBeginTimer() {
return self::$begin_timer;
}
/**
* 获取结束时间
* @return type
*/
public static function getFinishTimer() {
return self::$finish_timer;
}
/**
* 获取带微妙的时间戳
* @return type
*/
private static function getTime() {
list($usec, $sec) = explode(" ", microtime());
return (number_format($sec+$usec,6,'.', ''));
}
}
function computeTime($otime,$message){
return;
$ntime = xdebug_time_index();
$str = '';
$str .= $message . ($ntime*1000-$otime*1000);
$str .= "<br>";
echo $str;
}
function getMemoryUsed(){
$str = '消耗内存<h3 style="color:red"> ';
$str .= round(memory_get_usage()/1024/1024, 4).'MB';
$str .= '</h3>';
echo $str;
}
$count_i = 100*10000;
//$count_i = 100000;
Timer_profiler::setBeginTimer();
computeTime(xdebug_time_index(),'开始执行');
echo Timer_profiler::recordHtml('开始执行');
getMemoryUsed();
class TestVar {
public $A1 = 'aaaaaaaaaaaaaaaaa';
public $A2 = 'aaaaaaaaaaaaaaaaa';
public $A3 = 'aaaaaaaaaaaaaaaaa';
public $A4 = 'aaaaaaaaaaaaaaaaa';
public $A5 = 'aaaaaaaaaaaaaaaaa';
public $A6 = 'aaaaaaaaaaaaaaaaa';
public $A7 = 'aaaaaaaaaaaaaaaaa';
public $A8 = 'aaaaaaaaaaaaaaaaa';
}
$TestVar = new TestVar();
for($i=0;$i<=$count_i;$i++){
$t = $TestVar->A1;
$t = $TestVar->A2;
$t = $TestVar->A3;
$t = $TestVar->A4;
$t = $TestVar->A5;
$t = $TestVar->A6;
$t = $TestVar->A7;
$t = $TestVar->A8;
}
getMemoryUsed();
echo Timer_profiler::recordHtml('变量完成');
computeTime(xdebug_time_index(),'变量完成');
Timer_profiler::setBeginTimer();
class TestStaticVar {
static $A1 = 'aaaaaaaaaaaaaaaaa';
static $A2 = 'aaaaaaaaaaaaaaaaa';
static $A3 = 'aaaaaaaaaaaaaaaaa';
static $A4 = 'aaaaaaaaaaaaaaaaa';
static $A5 = 'aaaaaaaaaaaaaaaaa';
static $A6 = 'aaaaaaaaaaaaaaaaa';
static $A7 = 'aaaaaaaaaaaaaaaaa';
static $A8 = 'aaaaaaaaaaaaaaaaa';
}
for($i=0;$i<=$count_i;$i++){
$t = TestStaticVar::$A1;
$t = TestStaticVar::$A2;
$t = TestStaticVar::$A3;
$t = TestStaticVar::$A4;
$t = TestStaticVar::$A5;
$t = TestStaticVar::$A6;
$t = TestStaticVar::$A7;
$t = TestStaticVar::$A8;
}
getMemoryUsed();
echo Timer_profiler::recordHtml('静态变量完成');
computeTime(xdebug_time_index(),'静态变量完成');
Timer_profiler::setBeginTimer();
class TestConstVar {
const A1 = 'aaaaaaaaaaaaaaaaa';
const A2 = 'aaaaaaaaaaaaaaaaa';
const A3 = 'aaaaaaaaaaaaaaaaa';
const A4 = 'aaaaaaaaaaaaaaaaa';
const A5 = 'aaaaaaaaaaaaaaaaa';
const A6 = 'aaaaaaaaaaaaaaaaa';
const A7 = 'aaaaaaaaaaaaaaaaa';
const A8 = 'aaaaaaaaaaaaaaaaa';
}
for($i=0;$i<=$count_i;$i++){
$t = TestConstVar::A1;
$t = TestConstVar::A2;
$t = TestConstVar::A3;
$t = TestConstVar::A4;
$t = TestConstVar::A5;
$t = TestConstVar::A6;
$t = TestConstVar::A7;
$t = TestConstVar::A8;
}
getMemoryUsed();
echo Timer_profiler::recordHtml('常量完成');
computeTime(xdebug_time_index(),'常量完成');
//echo Timer_profiler::recordHtml('共执行');
computeTime(xdebug_time_index(),'共执行');
exit;
评论