在对类执行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. Faster-RCNN论文精读

    State-of-the-art object detection networks depend on region proposal algorithms to hypothesize objec ...

  2. Delphi Close、Halt、terminate、ExitProcess的区别

    Close:1.只关闭本窗体2.当Close是一个主窗体时,程序会退出.3.Close会发生FormClose事件,FormCloseQuery事件4.主窗体close以后程序就Application ...

  3. 启动php-fpm和nginx

    /usr/local/php/sbin/php-fpm #手动打补丁的启动方式/usr/local/php/sbin/php-fpm start sudo /usr/local/nginx/nginx ...

  4. 【LeetCode 7】整数反转

    题目链接 [题解] 没什么说的. 就注意一点. 可以在*10+n%10的时候. 顺便判断有没有溢出. (直接用longlong可真是机制..) [代码] class Solution { public ...

  5. [NOIP模拟测试11] 题解

    A.string 和河北的一道省选题很像.考场上写的暴力桶排,正解其实就是优化一下这个思路. 开线段树维护字符串中每个字母出现的次数.对于每条询问,区间查询.区间赋值维护即可. 另外,本题卡常严重,正 ...

  6. Java-Class-I:org.springframework.web.mutipart.MutipartFile

    ylbtech-Java-Class-I:org.springframework.web.mutipart.MutipartFile 1.返回顶部   2.返回顶部 1.1. import org.s ...

  7. CSS:CSS 媒体类型

    ylbtech-CSS:CSS 媒体类型 1.返回顶部 1. CSS 媒体类型 媒体类型允许你指定文件将如何在不同媒体呈现.该文件可以以不同的方式显示在屏幕上,在纸张上,或听觉浏览器等等. 媒体类型 ...

  8. CentOS6.8搭建LNMP环境

    selinux可能会致使编译安装失败,我们先禁用它.永久禁用,需要重启生效 sed -i ‘s/SELINUX=enforcing/SELINUX=disabled/g’ /etc/selinux/c ...

  9. TLS/SSL 协议 - ServerHello

    ServerHello ServerHello消息的意义是将服务器选择的连接参数传送回客户端.这个消息的结构与ClientHello类似,只是每个字段只包含一个选项. 服务器无需支持客户端支持的最佳版 ...

  10. PAT_A1023#Have Fun with Numbers

    Source: PAT A1023 Have Fun with Numbers (20 分) Description: Notice that the number 123456789 is a 9- ...