雖然我是個PHP新手(沒拿PHP做過事),但是今天看到一段代碼,忍不住修改了幾行。

          代碼如下:
          class runTime {
              var $StartTime = 0;
              var $StopTime = 0;
              var $TimeSpent = 0;

              function start(){
                  $this->StartTime = microtime();
              }

              function stop(){
                  $this->StopTime = microtime();
              }

              function spent() {
                  if ($this->TimeSpent) {
                      return $this->TimeSpent;
                  } else {
                      $StartMicro = substr($this->StartTime,0,10);
                      $StartSecond = substr($this->StartTime,11,10);
                      $StopMicro = substr($this->StopTime,0,10);
                      $StopSecond = substr($this->StopTime,11,10);
                      $start = floatval($StartMicro) + $StartSecond;
                      $stop = floatval($StopMicro) + $StopSecond;
                      $this->TimeSpent = $stop - $start;
                      return round($this->TimeSpent,8);
                  }
              } // end function
          }

          1。為什么說封裝欠妥?

          在使用過程中,我發(fā)現(xiàn)那幾個類的屬性,沒必要作為var (public )形式出現(xiàn),既然用了class,那么就遵照下面向?qū)ο蟮囊恍┗疽?guī)則,這幾個變量完全可以用private 訪問控制。 

          2。 microtime 用得不夠好?

          手冊上關(guān)于microtime 的一些說明:

          定義和用法

          microtime() 函數(shù)返回當(dāng)前 Unix 時間戳和微秒數(shù)。

          如果調(diào)用時不帶可選參數(shù),本函數(shù)以 "msec sec" 的格式返回一個字符串,其中 sec 是自 Unix 紀(jì)元(0:00:00 January 1, 1970 GMT)起到現(xiàn)在的秒數(shù),msec 是微秒部分。字符串的兩部分都是以秒為單位返回的。

           


          在PHP5 以上版本,是可以接受參數(shù)true,這樣就能直接返回浮點數(shù),而且效率會比現(xiàn)在這樣做高不少。

          下面是網(wǎng)上找到的一段小代碼,可以做參考:

          <?php 
          function microtime_float3(){ 
              return microtime(true); 


          function microtime_float2(){ 
              ifPHP_VERSION > 5){ 
                  return microtime(true); 
              }else
                  list($usec$sec) = explode(" ", microtime()); 
                  return ((float)$usec + (float)$sec); 
              } 


          function microtime_float(){ 
              list($usec$sec) = explode(" ", microtime()); 
              return ((float)$usec + (float)$sec); 


          function runtime($t1){ 
              return number_format((microtime_float() - $t1)*1000, 4).'ms'; 


          $t1 = microtime_float(); 
          for($i=0;$i<10000;$i++){ 
              microtime_float(); 

          echo "microtime_float====="; 
          echo runtime($t1).'<br>'; 
          $t1 = microtime(true); 

          for($i=0;$i<10000;$i++){ 
              microtime(true); 

          echo "microtime_true====="; 
          echo runtime($t1).'<br>'; 
          $t1 = microtime(true); 

          for($i=0;$i<10000;$i++){ 
              microtime_float2(); 


          echo "microtime_float2====="; 
          echo runtime($t1).'<br>'; 
          $t1 = microtime(true); 

          for($i=0;$i<10000;$i++){
              microtime_float3(); 

          echo "microtime_float3====="; 
          echo runtime($t1).'<br>'; 
          ?> 

          本機winxp運行結(jié)果: 
          microtime_float=====109.5631ms 
          microtime_true=====38.8160ms 
          microtime_float2=====52.7902ms 
          microtime_float3=====45.0699ms 
          Linux上運行結(jié)果: 
          microtime_float=====47.2510ms 
          microtime_true=====9.2051ms 
          microtime_float2=====16.3319ms 
          microtime_float3=====12.2800ms 
          posted on 2011-11-15 00:17 -274°C 閱讀(2163) 評論(0)  編輯  收藏 所屬分類: PHP

          常用鏈接

          留言簿(21)

          隨筆分類(265)

          隨筆檔案(242)

          相冊

          JAVA網(wǎng)站

          關(guān)注的Blog

          搜索

          •  

          積分與排名

          • 積分 - 916102
          • 排名 - 40

          最新評論

          主站蜘蛛池模板: 隆安县| 墨竹工卡县| 秀山| 长宁县| 双辽市| 寿阳县| 徐闻县| 若羌县| 聂荣县| 称多县| 万安县| 来宾市| 安岳县| 合阳县| 姜堰市| 蒙阴县| 丹江口市| 凤凰县| 赣州市| 黄冈市| 乌审旗| 克拉玛依市| 和田县| 楚雄市| 郁南县| 涿鹿县| 博湖县| 仙居县| 石泉县| 扎赉特旗| 江油市| 安图县| 三明市| 云阳县| 河北区| 富锦市| 嵊泗县| 长沙县| 东莞市| 利川市| 黄平县|