php 类静态变量 和 常量消耗内存及时间对比

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

 

 

 

转载于:https://www.cnblogs.com/simon-sun/p/3412309.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/377595.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

一个机器周期 计算机_计算机科学组织| 机器周期

一个机器周期 计算机机器周期 (Machine Cycle) The cycle during which a machine language instruction is executed by the processor of the computer system is known as the machine cycle. If a program contains 10 machine language instruction, 10 separate machine …

四、Transforms

transform是torchvision下的一个.py文件&#xff0c;这个python文件中定义了很多的类和方法&#xff0c;主要实现对图片进行一些变换操作 一、Transforms讲解 from torchvision import transforms#按着Ctrl&#xff0c;点击transforms进入到__init__.py文件中 from .transfo…

leetcode 134. 加油站 思考分析

目录题目1、暴力法&#xff0c;双层遍历2、贪心题目 在一条环路上有 N 个加油站&#xff0c;其中第 i 个加油站有汽油 gas[i] 升。 你有一辆油箱容量无限的的汽车&#xff0c;从第 i 个加油站开往第 i1 个加油站需要消耗汽油 cost[i] 升。你从其中的一个加油站出发&#xff0…

单链线性表的实现

//函数结果状态代码#define TRUE 1 #define FALSE 0 #define OK 1 #define ERROR 0 #define INFEASIBLE -1 #define OVERFLOW -2 //Status是函数的类型&#xff0c;其值是函数结果状态代码 typedef int Status; typedef int ElemType;…

时间模块,带Python示例

Python时间模块 (Python time Module) The time module is a built-in module in Python and it has various functions that require to perform more operations on time. This is one of the best modules in Python that used to solve various real-life time-related pro…

五、torchvision

一、下载CIFAR-10数据集 CIFAR-10数据集官网 通过阅读官网给的解释可以大概了解到&#xff0c;一共6w张图片&#xff0c;每张图片大小为3232&#xff0c;5w张训练图像&#xff0c;1w张测试图像&#xff0c;一共由十大类图像。 CIFAR10官网使用文档 torchvision.datasets.CIF…

leetcode 69. x 的平方根 思考分析

题目 实现 int sqrt(int x) 函数。 计算并返回 x 的平方根&#xff0c;其中 x 是非负整数。 由于返回类型是整数&#xff0c;结果只保留整数的部分&#xff0c;小数部分将被舍去。 示例 1: 输入: 4 输出: 2 示例 2: 输入: 8 输出: 2 说明: 8 的平方根是 2.82842…, 由于返回…

背包问题 小灰_小背包问题

背包问题 小灰Prerequisites: Algorithm for fractional knapsack problem 先决条件&#xff1a; 分数背包问题算法 Here, we are discussing the practical implementation of the fractional knapsack problem. It can be solved using the greedy approach and in fraction…

360浏览器兼容问题

360浏览器兼容问题 360浏览器又是一大奇葩&#xff0c;市场份额大&#xff0c;让我们不得不也对他做些兼容性处理。 360浏览器提供了两种浏览模式&#xff0c;极速模式和兼容模式&#xff0c;极速模式下是webkit内核的处理模式&#xff0c;兼容模式下是与IE内核相同的处理模式。…

转 设计师也需要了解的一些前端知识

一、常见视觉效果是如何实现的 一些事 关于文字效果 互联网的一些事 文字自身属性相关的效果css中都是有相对应的样式的&#xff0c;如字号、行高、加粗、倾斜、下划线等&#xff0c;但是一些特殊的效果&#xff0c;主要表现为ps中图层样式中的效果&#xff0c;css是无能为力的…

六、DataLoader

一、DataLoader参数解析 DataLoader官网使用手册 参数描述dataset说明数据集所在的位置、数据总数等batch_size每次取多少张图片shuffleTrue乱序、False顺序(默认)samplerbatch_samplernum_workers多进程&#xff0c;默认为0采用主进程加载数据collate_fnpin_memorydrop_las…

单调栈 leetcode整理(一)

目录单调栈知识402. 移掉K位数字1673. 找出最具竞争力的子序列316. 去除重复字母&#xff08;1081. 不同字符的最小子序列&#xff09;321. 拼接最大数单调栈知识 单调栈就是一个内部元素有序的栈&#xff08;大->小 or 小->大&#xff09;&#xff0c;但是只用到它的一…

数字签名 那些密码技术_密码学中的数字签名

数字签名 那些密码技术A signature is usually used to bind signatory to the message. The digital signature is thus a technique that binds a person or the entity to the digital data. This binding ensures that the person sending the data is solely responsible …

七、torch.nn

一、神经网络模块 进入到PyTorch的torch.nnAPI学习页面 PyTorch提供了很多的神经网络方面的模块&#xff0c;NN就是Neural Networks的简称 二、Containers torch.nn下的Containers 一共有六个模块&#xff0c;最常用的就是Module模块&#xff0c;看解释可以知道&#xff0c…

Java多线程初学者指南(8):从线程返回数据的两种方法

本文介绍学习Java多线程中需要学习的从线程返回数据的两种方法。从线程中返回数据和向线程传递数据类似。也可以通过类成员以及回调函数来返回数据。原文链接 从线程中返回数据和向线程传递数据类似。也可以通过类成员以及回调函数来返回数据。但类成员在返回数据和传递数据时有…

【C++进阶】 遵循TDD原则,实现平面向量类(Vec2D)

目录1、明确要实现的类的方法以及成员函数2、假设已经编写Vec2D&#xff0c;根据要求&#xff0c;写出测试代码3、编写平面向量类Vec2D,并进行测试4、完整代码5、最终结果1、明确要实现的类的方法以及成员函数 考虑到效率问题&#xff0c;我们一般将函数的参数设置为引用类型。…

Keilc的中断号计算方法

中断号码 (中断向量-3)/8转载于:https://www.cnblogs.com/yuqilihualuo/p/3423634.html

md5模式 签名_MD的完整形式是什么?

md5模式 签名医师&#xff1a;医学博士/常务董事 (MD: Doctor of Medicine / Managing Director) 1)医学博士&#xff1a;医学博士 (1) MD: Doctor of Medicine) MD is an abbreviation of a Doctor of Medicine degree. In the field of Medicine, it is the main academic de…

八、卷积层

一、Conv2d torch.nn.Conv2d官网文档 torch.nn.Conv2d(in_channels, out_channels, kernel_size, stride1, padding0, dilation1, groups1, biasTrue, padding_modezeros, deviceNone, dtypeNone) 参数解释官网详情说明in_channels输入的通道数&#xff0c;如果是彩色照片通道…

HTMl5结构元素:header

页眉header 页眉将是页面加载的第一个元素&#xff0c;包含了站点的标题、logo、网站导航等。<header> <div class"container_16"> <div class"logo"> <h1><a href"index.html"><strong>Real</st…