php測試時間函數

來源:互聯網
上載者:User

著作權聲明:原創作品,允許轉載,轉載時請務必以超連結形式標明文章原始出版、作者資訊和本聲明。否則將追究法律責任。http://blog.csdn.net/mayongzhan - 馬永占,myz,mayongzhan

一個簡單的測試程式已耗用時間的類及使用.我是參考h058的資料寫的.最近剛好讀到這文章.對測試高度興趣!
原理先讀取目前時間到變數1.然後執行程式.然後讀取目前時間到變數2.用2減1.得執行時間...

<!-- 類檔案 -->
<?php
/**
 * @name test.php
 * @date Fri Oct 19 00:58:41 CST 2007
 * @copyright 馬永占(MyZ)
 * @author 馬永占(MyZ)
 * @link http://blog.csdn.net/mayongzhan/
 */
/**
 * Test a program how long it do
 * 注意:使用時請迴圈多次,否則看不出效果
 * @package
 * @version 1.0
 */
class TestTime

 private $beginTime = 0; //開始時間
 private $endTime = 0; //結束時間
 
 public function begin()
 {
  $this->beginTime = microtime(true);
 }
 
 public function end()
 {
  $this->endTime = microtime(true);
 }
 
 public function keepTime()
 {
  return $this->endTime-$this->beginTime;
 }
}
?>

<!-- 兩個比較檔案之一 -->
<?php
echo "<span style='display:none;'>echo !</span>";
?>

<!-- 兩個比較檔案之二 -->
<?php
print "<span style='display:none;'>print !</span>";
?>

<!-- 主要測試檔案 -->
<?php
require("test.php");
$TestTime1 = new TestTime();
$TestTime1->begin();
for ($i = 0; $i<10000; $i++) {
 include("p1.php");
}
$TestTime1->end();
echo " echo:".$TestTime1->keepTime();
$TestTime2 = new TestTime();
$TestTime2->begin();
for ($i = 0; $i<10000; $i++) {
 include("p2.php");
}
$TestTime2->end();
echo " print:".$TestTime2->keepTime();
?>

<!-- 執行結果為: -->
echo:1.6508288383484 print:1.6367089748383
哪個高??其實差不多...

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.