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.pswp.cn/news/377595.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/377595.shtml
英文地址,請注明出處:http://en.pswp.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…