在对类执行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. 【leetcode】962. Maximum Width Ramp

    题目如下: Given an array A of integers, a ramp is a tuple (i, j) for which i < j and A[i] <= A[j]. ...

  2. linux环境安装opencv导入依赖报错问题

    linux环境通过pip安装opencv后,导入cv2时报错: 在centos和ubuntu环境下都遇到相同的问题.报错原因: 缺少共享库 有两种解决办法: 一.使用如下命令查看缺少得共享库 yum ...

  3. js过滤字符串中的html标签

    var str = 'add<a>daad</a><p>fsdada</p>' str.replace(/<[^<>]+>/g, ...

  4. delphi 窗体的位置和高宽度-TForm:Letf、Top、Width、Height、ClientWidth、ClientHeight

    delphi 窗体的位置和高宽度-TForm:Letf.Top.Width.Height.ClientWidth.ClientHeight [窗体的高度和宽度]: [客户区的高度和宽度]: [窗体在屏 ...

  5. python语法学习

    global关键字(内部作用域想要对外部作用域的变量进行修改) decator装饰器,说白了就是一个函数指针的传递 *arg,**kwarg, 分别为tuple,dic传递

  6. sql update语句

    如果要更新数据库表中的记录,我们就必须使用UPDATE语句. UPDATE语句的基本语法是: UPDATE <表名> SET 字段1=值1, 字段2=值2, ... WHERE ...; ...

  7. [NOIP模拟14]题解

    当垃圾已经成为一种常态233333 A.旋转子段 考场上的$n^2$手残少了20分,555  (主要是因为实在打不出来$n^3$的做法所以写不了对拍?ccc为什么考场上没有想起有reverse()这么 ...

  8. Future初次使用理解

    当客户端执行方法时,立即返回一个代理对象,此时代理对象没有数据,与此同时开启一个线程去构造真实对象并把真实对象替换掉代理对象(使用set方法).所以就会出现,客户端收到代理对象之后以为执行完了然后执行 ...

  9. C# 中如何输出双引号(转义字符的使用)

    实现效果: 输出这样的一个含有双引号的字符串 "hello" 方式一: 不用 @ 时转义      System.Console.WriteLine("\"he ...

  10. Hyperledger:常见加密算法分类列表

    算法原理查询:http://mathworld.wolfram.com   加密散列函数 (消息摘要算法,消息认证码,MD算法) Keyed-hash message authentication c ...