在对类执行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;

php 类静态变量 和 常量消耗内存及时间对比的更多相关文章

  1. 常量和静态变量会先载入内存后在进行执行php代码

    static $test=1;//在php执行前就已经写入内存$test++;var_dump($test);static $test=10;//在php执行前就已经写入内存var_dump($tes ...

  2. PHP接口中的静态变量、常量与类中静态变量、常量的区别

    接口: 1 不能够定义静态变量(常量除外) 2 定义的常量 const YOUCONST = VALUE,不能在子类中覆盖,在子类中以 interfaceName::YOUCONST的方式调用 3 不 ...

  3. PHP 类属性 类静态变量的访问

    php的类属性其实有两种,一种是类常量,一种是类静态变量.两种容易引起混淆. 如同静态类方法和类实例方法一样,静态类属性和实例属性不能重定义(同名),但静态属性可以和类常量同名. <?php c ...

  4. python类静态变量

    python的类静态变量直接定义在类中即可,不需要修饰符,如: 1 class Test: stc_attr = 1 def __init__(self,attr1,attr2): self.attr ...

  5. oc 基本语法 类 静态变量 常量

    // // ReViewClass.h // hellowWorld // 本类是oc复习练手类 // Created by hongtao on 2018/3/26. // Copyright © ...

  6. Java 静态变量,常量和方法

    static 关键字 例如:在球类中使用PI这个常量,可能除了本类需要这个常量之外,在另外一个圆类中也需要使用这个常量.这时没有必要 在两个类中同时创建PI这个常量,因为这样系统会将这两个不在同一个类 ...

  7. Java静态变量,常量,成员变量,局部变量

    类变量(也叫静态变量)是类中独立于方法之外的变量,用static 修饰.(static表示“全局的”.“静态的”,用来修饰成员变量和成员方法,或静态代码块(静态代码块独立于类成员,jvm加载类时会执行 ...

  8. PHP:第一章——PHP中静态变量和常量

    <?php header("Content-Type:text/html;charset=utf-8"); /******************************** ...

  9. C++类静态变量的一种使用特例

    不同进程里的数据默认情况下是互不影响的. 静态变量是属于类本身的,它的所有实例可以共享这个静态变量,但是有个先天条件就是在同一个进程的情况下!!

随机推荐

  1. SQL Select选择

    SQL Select(选择) 语法 SELECT 语法用于从数据库中选择数据. 返回的数据存储在结果表中,称为结果集. SQL SELECT 语法 SELECT column1, column2, . ...

  2. 对AngularJs的简单了解

    一.简单介绍 AngularJS是为了克服HTML在构建应用上的不足而设计的.HTML是一门很好的为静态文本展示设计的声明式语言,但要构建WEB应用的话它就显得乏力了.所以我做了一些工作(你也可以觉得 ...

  3. 首次公开!单日600PB的计算力--阿里巴巴EB级大数据平台的进击

    MaxCompute作为阿里巴巴的主力计算平台,在2018年的双11中,再次不负众望,经受住了双11期间海量数据和高并发量的考验.为集团的各条业务线提供了强劲的计算力,不愧是为阿里巴巴历年双11输送超 ...

  4. 数字IT基础-数据采集总线

    摘要: 日志服务是阿里自产自用的产品,在双十一.双十二和新春红包期间承载阿里云/蚂蚁全站.阿里电商板块.云上几千商家数据链路,每日处理来自百万节点几十PB数据,峰值流量达到每秒百GB, 具备稳定.可靠 ...

  5. NOIP模拟测试29(A)

    T1: 题目大意:有一张有向无环图,第$x$次经过边$i$的代价为$a_ix+b_i$,最多经过$c_i$次,起点为1,$s$个点可作为终点,求走$k$次的最小代价. 我们新建一个汇点,将所有可做为终 ...

  6. (转)Vmware vSphere 5.0系列教程 vSphere网络原理及vSwitch简介 及一个host两个网卡说明

    转:http://andygao.blog.51cto.com/323260/817518/ 在一个物理网络拓扑中,通常都是路由器-交换机-PC机的连接,不同的服务器和PC机,通过交换机的连接而相互连 ...

  7. 【SQL】语句/函数汇总

    1.CHARINDEX(短字符A,长字符B) 说明:返回A在B的位置,从1开始,若B中不存在A,则为0 例如: SELECT CHARINDEX('aaaa','abaaaacded')  ----- ...

  8. PHP面试 PHP基础知识 八(会话控制)

    ---恢复内容开始--- PHP会话控制技术 首先了解一下为什么要使用会话控制技术? 本身web 与服务器的交互是通过HTTP协议来实现的,而HTTP协议又是无状态协议.就是说明HTTP协议没有一个內 ...

  9. HDU 6628 permutation 1 (暴力)

    2019 杭电多校 5 1005 题目链接:HDU 6628 比赛链接:2019 Multi-University Training Contest 5 Problem Description A s ...

  10. \t \r \n转义字符

    t \r \n都是转义字符,空格就是单纯的空格,输入时可以输入空格 \t 的意思是 横向跳到下一制表符位置 \r 的意思是 回车 \n 的意思是回车换行 所有的转义字符和所对应的意义: 转义字符 意义 ...