Hướng dẫn what is php execution time? - thời gian thực hiện php là gì?

Xem thảo luận

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • Xem thảo luận

    Cải thiện bài viết

    Lưu bài viết

    Đọc

    Bàn luận Sample script

    Thời gian thực hiện tập lệnh trong PHP là thời gian cần thiết để thực thi tập lệnh PHP. Để tính toán thời gian thực hiện tập lệnh sử dụng thời gian đồng hồ thay vì thời gian thực hiện CPU. Biết thời gian đồng hồ trước khi thực thi tập lệnh và sau khi thực thi tập lệnh sẽ giúp biết thời gian thực hiện tập lệnh.

    namespace g3;
    
    class Utils {
       public function __construct() {}
    
       public static $UtilsDtStart = [];
       public static $UtilsDtStats = [];
    
       public static function dt() {
          global $UtilsDtStart, $UtilsDtStats;
          $obj = new \stdClass();
          $obj->start = function(int $ndx = 0) use (&$UtilsDtStart) {
             $UtilsDtStart[$ndx] = \microtime(true) * 1000;
          };
          $obj->codeStart = function(int $ndx = 0) use (&$UtilsDtStart) {
             $use = \getrusage();
             $UtilsDtStart[$ndx] = ($use["ru_utime.tv_sec"] * 1000) + ($use["ru_utime.tv_usec"] / 1000);
          };
          $obj->resourceStart = function(int $ndx = 0) use (&$UtilsDtStart) {
             $use = \getrusage();
             $UtilsDtStart[$ndx] = $use["ru_stime.tv_usec"] / 1000;
          };
          $obj->end = function(int $ndx = 0) use (&$UtilsDtStart, &$UtilsDtStats) {
             $t = @$UtilsDtStart[$ndx];
             if($t === null)
                return false;
             $end = \microtime(true) * 1000;
             $dt = $end - $t;
             $UtilsDtStats[$ndx][] = $dt;
             return $dt;
          };
          $obj->codeEnd = function(int $ndx = 0) use (&$UtilsDtStart, &$UtilsDtStats) {
             $t = @$UtilsDtStart[$ndx];
             if($t === null)
                return false;
             $use = \getrusage();
             $dt = ($use["ru_utime.tv_sec"] * 1000) + ($use["ru_utime.tv_usec"] / 1000) - $t;
             $UtilsDtStats[$ndx][] = $dt;
             return $dt;
          };
          $obj->resourceEnd = function(int $ndx = 0) use (&$UtilsDtStart, &$UtilsDtStats) {
             $t = @$UtilsDtStart[$ndx];
             if($t === null)
                return false;
             $use = \getrusage();
             $dt = ($use["ru_stime.tv_usec"] / 1000) - $t;
             $UtilsDtStats[$ndx][] = $dt;
             return $dt;
          };
          $obj->stats = function(int $ndx = 0) use (&$UtilsDtStats) {
             $s = @$UtilsDtStats[$ndx];
             if($s !== null)
                $s = \array_slice($s, 0);
             else
                $s = false;
             return $s;
          };
          $obj->statsLength = function() use (&$UtilsDtStats) {
             return \count($UtilsDtStats);
          };
          return $obj;
       }
    }
    
    3

    Ví dụ: tập lệnh mẫu

    namespace g3;
    
    class Utils {
       public function __construct() {}
    
       public static $UtilsDtStart = [];
       public static $UtilsDtStats = [];
    
       public static function dt() {
          global $UtilsDtStart, $UtilsDtStats;
          $obj = new \stdClass();
          $obj->start = function(int $ndx = 0) use (&$UtilsDtStart) {
             $UtilsDtStart[$ndx] = \microtime(true) * 1000;
          };
          $obj->codeStart = function(int $ndx = 0) use (&$UtilsDtStart) {
             $use = \getrusage();
             $UtilsDtStart[$ndx] = ($use["ru_utime.tv_sec"] * 1000) + ($use["ru_utime.tv_usec"] / 1000);
          };
          $obj->resourceStart = function(int $ndx = 0) use (&$UtilsDtStart) {
             $use = \getrusage();
             $UtilsDtStart[$ndx] = $use["ru_stime.tv_usec"] / 1000;
          };
          $obj->end = function(int $ndx = 0) use (&$UtilsDtStart, &$UtilsDtStats) {
             $t = @$UtilsDtStart[$ndx];
             if($t === null)
                return false;
             $end = \microtime(true) * 1000;
             $dt = $end - $t;
             $UtilsDtStats[$ndx][] = $dt;
             return $dt;
          };
          $obj->codeEnd = function(int $ndx = 0) use (&$UtilsDtStart, &$UtilsDtStats) {
             $t = @$UtilsDtStart[$ndx];
             if($t === null)
                return false;
             $use = \getrusage();
             $dt = ($use["ru_utime.tv_sec"] * 1000) + ($use["ru_utime.tv_usec"] / 1000) - $t;
             $UtilsDtStats[$ndx][] = $dt;
             return $dt;
          };
          $obj->resourceEnd = function(int $ndx = 0) use (&$UtilsDtStart, &$UtilsDtStats) {
             $t = @$UtilsDtStart[$ndx];
             if($t === null)
                return false;
             $use = \getrusage();
             $dt = ($use["ru_stime.tv_usec"] / 1000) - $t;
             $UtilsDtStats[$ndx][] = $dt;
             return $dt;
          };
          $obj->stats = function(int $ndx = 0) use (&$UtilsDtStats) {
             $s = @$UtilsDtStats[$ndx];
             if($s !== null)
                $s = \array_slice($s, 0);
             else
                $s = false;
             return $s;
          };
          $obj->statsLength = function() use (&$UtilsDtStats) {
             return \count($UtilsDtStats);
          };
          return $obj;
       }
    }
    
    8

    namespace g3;
    
    class Utils {
       public function __construct() {}
    
       public static $UtilsDtStart = [];
       public static $UtilsDtStats = [];
    
       public static function dt() {
          global $UtilsDtStart, $UtilsDtStats;
          $obj = new \stdClass();
          $obj->start = function(int $ndx = 0) use (&$UtilsDtStart) {
             $UtilsDtStart[$ndx] = \microtime(true) * 1000;
          };
          $obj->codeStart = function(int $ndx = 0) use (&$UtilsDtStart) {
             $use = \getrusage();
             $UtilsDtStart[$ndx] = ($use["ru_utime.tv_sec"] * 1000) + ($use["ru_utime.tv_usec"] / 1000);
          };
          $obj->resourceStart = function(int $ndx = 0) use (&$UtilsDtStart) {
             $use = \getrusage();
             $UtilsDtStart[$ndx] = $use["ru_stime.tv_usec"] / 1000;
          };
          $obj->end = function(int $ndx = 0) use (&$UtilsDtStart, &$UtilsDtStats) {
             $t = @$UtilsDtStart[$ndx];
             if($t === null)
                return false;
             $end = \microtime(true) * 1000;
             $dt = $end - $t;
             $UtilsDtStats[$ndx][] = $dt;
             return $dt;
          };
          $obj->codeEnd = function(int $ndx = 0) use (&$UtilsDtStart, &$UtilsDtStats) {
             $t = @$UtilsDtStart[$ndx];
             if($t === null)
                return false;
             $use = \getrusage();
             $dt = ($use["ru_utime.tv_sec"] * 1000) + ($use["ru_utime.tv_usec"] / 1000) - $t;
             $UtilsDtStats[$ndx][] = $dt;
             return $dt;
          };
          $obj->resourceEnd = function(int $ndx = 0) use (&$UtilsDtStart, &$UtilsDtStats) {
             $t = @$UtilsDtStart[$ndx];
             if($t === null)
                return false;
             $use = \getrusage();
             $dt = ($use["ru_stime.tv_usec"] / 1000) - $t;
             $UtilsDtStats[$ndx][] = $dt;
             return $dt;
          };
          $obj->stats = function(int $ndx = 0) use (&$UtilsDtStats) {
             $s = @$UtilsDtStats[$ndx];
             if($s !== null)
                $s = \array_slice($s, 0);
             else
                $s = false;
             return $s;
          };
          $obj->statsLength = function() use (&$UtilsDtStats) {
             return \count($UtilsDtStats);
          };
          return $obj;
       }
    }
    
    9

    for($i = 1; $i

    Example:

    namespace g3;
    
    class Utils {
       public function __construct() {}
    
       public static $UtilsDtStart = [];
       public static $UtilsDtStats = [];
    
       public static function dt() {
          global $UtilsDtStart, $UtilsDtStats;
          $obj = new \stdClass();
          $obj->start = function(int $ndx = 0) use (&$UtilsDtStart) {
             $UtilsDtStart[$ndx] = \microtime(true) * 1000;
          };
          $obj->codeStart = function(int $ndx = 0) use (&$UtilsDtStart) {
             $use = \getrusage();
             $UtilsDtStart[$ndx] = ($use["ru_utime.tv_sec"] * 1000) + ($use["ru_utime.tv_usec"] / 1000);
          };
          $obj->resourceStart = function(int $ndx = 0) use (&$UtilsDtStart) {
             $use = \getrusage();
             $UtilsDtStart[$ndx] = $use["ru_stime.tv_usec"] / 1000;
          };
          $obj->end = function(int $ndx = 0) use (&$UtilsDtStart, &$UtilsDtStats) {
             $t = @$UtilsDtStart[$ndx];
             if($t === null)
                return false;
             $end = \microtime(true) * 1000;
             $dt = $end - $t;
             $UtilsDtStats[$ndx][] = $dt;
             return $dt;
          };
          $obj->codeEnd = function(int $ndx = 0) use (&$UtilsDtStart, &$UtilsDtStats) {
             $t = @$UtilsDtStart[$ndx];
             if($t === null)
                return false;
             $use = \getrusage();
             $dt = ($use["ru_utime.tv_sec"] * 1000) + ($use["ru_utime.tv_usec"] / 1000) - $t;
             $UtilsDtStats[$ndx][] = $dt;
             return $dt;
          };
          $obj->resourceEnd = function(int $ndx = 0) use (&$UtilsDtStart, &$UtilsDtStats) {
             $t = @$UtilsDtStart[$ndx];
             if($t === null)
                return false;
             $use = \getrusage();
             $dt = ($use["ru_stime.tv_usec"] / 1000) - $t;
             $UtilsDtStats[$ndx][] = $dt;
             return $dt;
          };
          $obj->stats = function(int $ndx = 0) use (&$UtilsDtStats) {
             $s = @$UtilsDtStats[$ndx];
             if($s !== null)
                $s = \array_slice($s, 0);
             else
                $s = false;
             return $s;
          };
          $obj->statsLength = function() use (&$UtilsDtStats) {
             return \count($UtilsDtStats);
          };
          return $obj;
       }
    }
    
    4
    namespace g3;
    
    class Utils {
       public function __construct() {}
    
       public static $UtilsDtStart = [];
       public static $UtilsDtStats = [];
    
       public static function dt() {
          global $UtilsDtStart, $UtilsDtStats;
          $obj = new \stdClass();
          $obj->start = function(int $ndx = 0) use (&$UtilsDtStart) {
             $UtilsDtStart[$ndx] = \microtime(true) * 1000;
          };
          $obj->codeStart = function(int $ndx = 0) use (&$UtilsDtStart) {
             $use = \getrusage();
             $UtilsDtStart[$ndx] = ($use["ru_utime.tv_sec"] * 1000) + ($use["ru_utime.tv_usec"] / 1000);
          };
          $obj->resourceStart = function(int $ndx = 0) use (&$UtilsDtStart) {
             $use = \getrusage();
             $UtilsDtStart[$ndx] = $use["ru_stime.tv_usec"] / 1000;
          };
          $obj->end = function(int $ndx = 0) use (&$UtilsDtStart, &$UtilsDtStats) {
             $t = @$UtilsDtStart[$ndx];
             if($t === null)
                return false;
             $end = \microtime(true) * 1000;
             $dt = $end - $t;
             $UtilsDtStats[$ndx][] = $dt;
             return $dt;
          };
          $obj->codeEnd = function(int $ndx = 0) use (&$UtilsDtStart, &$UtilsDtStats) {
             $t = @$UtilsDtStart[$ndx];
             if($t === null)
                return false;
             $use = \getrusage();
             $dt = ($use["ru_utime.tv_sec"] * 1000) + ($use["ru_utime.tv_usec"] / 1000) - $t;
             $UtilsDtStats[$ndx][] = $dt;
             return $dt;
          };
          $obj->resourceEnd = function(int $ndx = 0) use (&$UtilsDtStart, &$UtilsDtStats) {
             $t = @$UtilsDtStart[$ndx];
             if($t === null)
                return false;
             $use = \getrusage();
             $dt = ($use["ru_stime.tv_usec"] / 1000) - $t;
             $UtilsDtStats[$ndx][] = $dt;
             return $dt;
          };
          $obj->stats = function(int $ndx = 0) use (&$UtilsDtStats) {
             $s = @$UtilsDtStats[$ndx];
             if($s !== null)
                $s = \array_slice($s, 0);
             else
                $s = false;
             return $s;
          };
          $obj->statsLength = function() use (&$UtilsDtStats) {
             return \count($UtilsDtStats);
          };
          return $obj;
       }
    }
    
    5
    namespace g3;
    
    class Utils {
       public function __construct() {}
    
       public static $UtilsDtStart = [];
       public static $UtilsDtStats = [];
    
       public static function dt() {
          global $UtilsDtStart, $UtilsDtStats;
          $obj = new \stdClass();
          $obj->start = function(int $ndx = 0) use (&$UtilsDtStart) {
             $UtilsDtStart[$ndx] = \microtime(true) * 1000;
          };
          $obj->codeStart = function(int $ndx = 0) use (&$UtilsDtStart) {
             $use = \getrusage();
             $UtilsDtStart[$ndx] = ($use["ru_utime.tv_sec"] * 1000) + ($use["ru_utime.tv_usec"] / 1000);
          };
          $obj->resourceStart = function(int $ndx = 0) use (&$UtilsDtStart) {
             $use = \getrusage();
             $UtilsDtStart[$ndx] = $use["ru_stime.tv_usec"] / 1000;
          };
          $obj->end = function(int $ndx = 0) use (&$UtilsDtStart, &$UtilsDtStats) {
             $t = @$UtilsDtStart[$ndx];
             if($t === null)
                return false;
             $end = \microtime(true) * 1000;
             $dt = $end - $t;
             $UtilsDtStats[$ndx][] = $dt;
             return $dt;
          };
          $obj->codeEnd = function(int $ndx = 0) use (&$UtilsDtStart, &$UtilsDtStats) {
             $t = @$UtilsDtStart[$ndx];
             if($t === null)
                return false;
             $use = \getrusage();
             $dt = ($use["ru_utime.tv_sec"] * 1000) + ($use["ru_utime.tv_usec"] / 1000) - $t;
             $UtilsDtStats[$ndx][] = $dt;
             return $dt;
          };
          $obj->resourceEnd = function(int $ndx = 0) use (&$UtilsDtStart, &$UtilsDtStats) {
             $t = @$UtilsDtStart[$ndx];
             if($t === null)
                return false;
             $use = \getrusage();
             $dt = ($use["ru_stime.tv_usec"] / 1000) - $t;
             $UtilsDtStats[$ndx][] = $dt;
             return $dt;
          };
          $obj->stats = function(int $ndx = 0) use (&$UtilsDtStats) {
             $s = @$UtilsDtStats[$ndx];
             if($s !== null)
                $s = \array_slice($s, 0);
             else
                $s = false;
             return $s;
          };
          $obj->statsLength = function() use (&$UtilsDtStats) {
             return \count($UtilsDtStats);
          };
          return $obj;
       }
    }
    
    6
    namespace g3;
    
    class Utils {
       public function __construct() {}
    
       public static $UtilsDtStart = [];
       public static $UtilsDtStats = [];
    
       public static function dt() {
          global $UtilsDtStart, $UtilsDtStats;
          $obj = new \stdClass();
          $obj->start = function(int $ndx = 0) use (&$UtilsDtStart) {
             $UtilsDtStart[$ndx] = \microtime(true) * 1000;
          };
          $obj->codeStart = function(int $ndx = 0) use (&$UtilsDtStart) {
             $use = \getrusage();
             $UtilsDtStart[$ndx] = ($use["ru_utime.tv_sec"] * 1000) + ($use["ru_utime.tv_usec"] / 1000);
          };
          $obj->resourceStart = function(int $ndx = 0) use (&$UtilsDtStart) {
             $use = \getrusage();
             $UtilsDtStart[$ndx] = $use["ru_stime.tv_usec"] / 1000;
          };
          $obj->end = function(int $ndx = 0) use (&$UtilsDtStart, &$UtilsDtStats) {
             $t = @$UtilsDtStart[$ndx];
             if($t === null)
                return false;
             $end = \microtime(true) * 1000;
             $dt = $end - $t;
             $UtilsDtStats[$ndx][] = $dt;
             return $dt;
          };
          $obj->codeEnd = function(int $ndx = 0) use (&$UtilsDtStart, &$UtilsDtStats) {
             $t = @$UtilsDtStart[$ndx];
             if($t === null)
                return false;
             $use = \getrusage();
             $dt = ($use["ru_utime.tv_sec"] * 1000) + ($use["ru_utime.tv_usec"] / 1000) - $t;
             $UtilsDtStats[$ndx][] = $dt;
             return $dt;
          };
          $obj->resourceEnd = function(int $ndx = 0) use (&$UtilsDtStart, &$UtilsDtStats) {
             $t = @$UtilsDtStart[$ndx];
             if($t === null)
                return false;
             $use = \getrusage();
             $dt = ($use["ru_stime.tv_usec"] / 1000) - $t;
             $UtilsDtStats[$ndx][] = $dt;
             return $dt;
          };
          $obj->stats = function(int $ndx = 0) use (&$UtilsDtStats) {
             $s = @$UtilsDtStats[$ndx];
             if($s !== null)
                $s = \array_slice($s, 0);
             else
                $s = false;
             return $s;
          };
          $obj->statsLength = function() use (&$UtilsDtStats) {
             return \count($UtilsDtStats);
          };
          return $obj;
       }
    }
    
    7

    File A
    ------
    \call_user_func_array(\g3\Utils::dt()->start, [0]);   // point A
    ...
    File B
    ------
    $dt = \call_user_func_array(\g3\Utils::dt()->end, [0]);  // point B
    
    3
    File A
    ------
    \call_user_func_array(\g3\Utils::dt()->start, [0]);   // point A
    ...
    File B
    ------
    $dt = \call_user_func_array(\g3\Utils::dt()->end, [0]);  // point B
    
    4

    Thời gian thực hiện tập lệnh trong PHP là thời gian cần thiết để thực thi tập lệnh PHP. Để tính toán thời gian thực hiện tập lệnh sử dụng thời gian đồng hồ thay vì thời gian thực hiện CPU. Biết thời gian đồng hồ trước khi thực thi tập lệnh và sau khi thực thi tập lệnh sẽ giúp biết thời gian thực hiện tập lệnh.

    namespace g3;
    
    class Utils {
       public function __construct() {}
    
       public static $UtilsDtStart = [];
       public static $UtilsDtStats = [];
    
       public static function dt() {
          global $UtilsDtStart, $UtilsDtStats;
          $obj = new \stdClass();
          $obj->start = function(int $ndx = 0) use (&$UtilsDtStart) {
             $UtilsDtStart[$ndx] = \microtime(true) * 1000;
          };
          $obj->codeStart = function(int $ndx = 0) use (&$UtilsDtStart) {
             $use = \getrusage();
             $UtilsDtStart[$ndx] = ($use["ru_utime.tv_sec"] * 1000) + ($use["ru_utime.tv_usec"] / 1000);
          };
          $obj->resourceStart = function(int $ndx = 0) use (&$UtilsDtStart) {
             $use = \getrusage();
             $UtilsDtStart[$ndx] = $use["ru_stime.tv_usec"] / 1000;
          };
          $obj->end = function(int $ndx = 0) use (&$UtilsDtStart, &$UtilsDtStats) {
             $t = @$UtilsDtStart[$ndx];
             if($t === null)
                return false;
             $end = \microtime(true) * 1000;
             $dt = $end - $t;
             $UtilsDtStats[$ndx][] = $dt;
             return $dt;
          };
          $obj->codeEnd = function(int $ndx = 0) use (&$UtilsDtStart, &$UtilsDtStats) {
             $t = @$UtilsDtStart[$ndx];
             if($t === null)
                return false;
             $use = \getrusage();
             $dt = ($use["ru_utime.tv_sec"] * 1000) + ($use["ru_utime.tv_usec"] / 1000) - $t;
             $UtilsDtStats[$ndx][] = $dt;
             return $dt;
          };
          $obj->resourceEnd = function(int $ndx = 0) use (&$UtilsDtStart, &$UtilsDtStats) {
             $t = @$UtilsDtStart[$ndx];
             if($t === null)
                return false;
             $use = \getrusage();
             $dt = ($use["ru_stime.tv_usec"] / 1000) - $t;
             $UtilsDtStats[$ndx][] = $dt;
             return $dt;
          };
          $obj->stats = function(int $ndx = 0) use (&$UtilsDtStats) {
             $s = @$UtilsDtStats[$ndx];
             if($s !== null)
                $s = \array_slice($s, 0);
             else
                $s = false;
             return $s;
          };
          $obj->statsLength = function() use (&$UtilsDtStats) {
             return \count($UtilsDtStats);
          };
          return $obj;
       }
    }
    
    3

    namespace g3;
    
    class Utils {
       public function __construct() {}
    
       public static $UtilsDtStart = [];
       public static $UtilsDtStats = [];
    
       public static function dt() {
          global $UtilsDtStart, $UtilsDtStats;
          $obj = new \stdClass();
          $obj->start = function(int $ndx = 0) use (&$UtilsDtStart) {
             $UtilsDtStart[$ndx] = \microtime(true) * 1000;
          };
          $obj->codeStart = function(int $ndx = 0) use (&$UtilsDtStart) {
             $use = \getrusage();
             $UtilsDtStart[$ndx] = ($use["ru_utime.tv_sec"] * 1000) + ($use["ru_utime.tv_usec"] / 1000);
          };
          $obj->resourceStart = function(int $ndx = 0) use (&$UtilsDtStart) {
             $use = \getrusage();
             $UtilsDtStart[$ndx] = $use["ru_stime.tv_usec"] / 1000;
          };
          $obj->end = function(int $ndx = 0) use (&$UtilsDtStart, &$UtilsDtStats) {
             $t = @$UtilsDtStart[$ndx];
             if($t === null)
                return false;
             $end = \microtime(true) * 1000;
             $dt = $end - $t;
             $UtilsDtStats[$ndx][] = $dt;
             return $dt;
          };
          $obj->codeEnd = function(int $ndx = 0) use (&$UtilsDtStart, &$UtilsDtStats) {
             $t = @$UtilsDtStart[$ndx];
             if($t === null)
                return false;
             $use = \getrusage();
             $dt = ($use["ru_utime.tv_sec"] * 1000) + ($use["ru_utime.tv_usec"] / 1000) - $t;
             $UtilsDtStats[$ndx][] = $dt;
             return $dt;
          };
          $obj->resourceEnd = function(int $ndx = 0) use (&$UtilsDtStart, &$UtilsDtStats) {
             $t = @$UtilsDtStart[$ndx];
             if($t === null)
                return false;
             $use = \getrusage();
             $dt = ($use["ru_stime.tv_usec"] / 1000) - $t;
             $UtilsDtStats[$ndx][] = $dt;
             return $dt;
          };
          $obj->stats = function(int $ndx = 0) use (&$UtilsDtStats) {
             $s = @$UtilsDtStats[$ndx];
             if($s !== null)
                $s = \array_slice($s, 0);
             else
                $s = false;
             return $s;
          };
          $obj->statsLength = function() use (&$UtilsDtStats) {
             return \count($UtilsDtStats);
          };
          return $obj;
       }
    }
    
    4
    File A
    ------
    \call_user_func_array(\g3\Utils::dt()->start, [0]);   // point A
    ...
    File B
    ------
    $dt = \call_user_func_array(\g3\Utils::dt()->end, [0]);  // point B
    
    3
    File A
    ------
    \call_user_func_array(\g3\Utils::dt()->codeStart, [1]);   // point A
    ...
    File B
    ------
    $dt = \call_user_func_array(\g3\Utils::dt()->codeEnd, [1]);  // point B
    
    6

    namespace g3;
    
    class Utils {
       public function __construct() {}
    
       public static $UtilsDtStart = [];
       public static $UtilsDtStats = [];
    
       public static function dt() {
          global $UtilsDtStart, $UtilsDtStats;
          $obj = new \stdClass();
          $obj->start = function(int $ndx = 0) use (&$UtilsDtStart) {
             $UtilsDtStart[$ndx] = \microtime(true) * 1000;
          };
          $obj->codeStart = function(int $ndx = 0) use (&$UtilsDtStart) {
             $use = \getrusage();
             $UtilsDtStart[$ndx] = ($use["ru_utime.tv_sec"] * 1000) + ($use["ru_utime.tv_usec"] / 1000);
          };
          $obj->resourceStart = function(int $ndx = 0) use (&$UtilsDtStart) {
             $use = \getrusage();
             $UtilsDtStart[$ndx] = $use["ru_stime.tv_usec"] / 1000;
          };
          $obj->end = function(int $ndx = 0) use (&$UtilsDtStart, &$UtilsDtStats) {
             $t = @$UtilsDtStart[$ndx];
             if($t === null)
                return false;
             $end = \microtime(true) * 1000;
             $dt = $end - $t;
             $UtilsDtStats[$ndx][] = $dt;
             return $dt;
          };
          $obj->codeEnd = function(int $ndx = 0) use (&$UtilsDtStart, &$UtilsDtStats) {
             $t = @$UtilsDtStart[$ndx];
             if($t === null)
                return false;
             $use = \getrusage();
             $dt = ($use["ru_utime.tv_sec"] * 1000) + ($use["ru_utime.tv_usec"] / 1000) - $t;
             $UtilsDtStats[$ndx][] = $dt;
             return $dt;
          };
          $obj->resourceEnd = function(int $ndx = 0) use (&$UtilsDtStart, &$UtilsDtStats) {
             $t = @$UtilsDtStart[$ndx];
             if($t === null)
                return false;
             $use = \getrusage();
             $dt = ($use["ru_stime.tv_usec"] / 1000) - $t;
             $UtilsDtStats[$ndx][] = $dt;
             return $dt;
          };
          $obj->stats = function(int $ndx = 0) use (&$UtilsDtStats) {
             $s = @$UtilsDtStats[$ndx];
             if($s !== null)
                $s = \array_slice($s, 0);
             else
                $s = false;
             return $s;
          };
          $obj->statsLength = function() use (&$UtilsDtStats) {
             return \count($UtilsDtStats);
          };
          return $obj;
       }
    }
    
    8

    Ví dụ: tập lệnh mẫu

    for($i = 1; $i

    namespace g3;
    
    class Utils {
       public function __construct() {}
    
       public static $UtilsDtStart = [];
       public static $UtilsDtStats = [];
    
       public static function dt() {
          global $UtilsDtStart, $UtilsDtStats;
          $obj = new \stdClass();
          $obj->start = function(int $ndx = 0) use (&$UtilsDtStart) {
             $UtilsDtStart[$ndx] = \microtime(true) * 1000;
          };
          $obj->codeStart = function(int $ndx = 0) use (&$UtilsDtStart) {
             $use = \getrusage();
             $UtilsDtStart[$ndx] = ($use["ru_utime.tv_sec"] * 1000) + ($use["ru_utime.tv_usec"] / 1000);
          };
          $obj->resourceStart = function(int $ndx = 0) use (&$UtilsDtStart) {
             $use = \getrusage();
             $UtilsDtStart[$ndx] = $use["ru_stime.tv_usec"] / 1000;
          };
          $obj->end = function(int $ndx = 0) use (&$UtilsDtStart, &$UtilsDtStats) {
             $t = @$UtilsDtStart[$ndx];
             if($t === null)
                return false;
             $end = \microtime(true) * 1000;
             $dt = $end - $t;
             $UtilsDtStats[$ndx][] = $dt;
             return $dt;
          };
          $obj->codeEnd = function(int $ndx = 0) use (&$UtilsDtStart, &$UtilsDtStats) {
             $t = @$UtilsDtStart[$ndx];
             if($t === null)
                return false;
             $use = \getrusage();
             $dt = ($use["ru_utime.tv_sec"] * 1000) + ($use["ru_utime.tv_usec"] / 1000) - $t;
             $UtilsDtStats[$ndx][] = $dt;
             return $dt;
          };
          $obj->resourceEnd = function(int $ndx = 0) use (&$UtilsDtStart, &$UtilsDtStats) {
             $t = @$UtilsDtStart[$ndx];
             if($t === null)
                return false;
             $use = \getrusage();
             $dt = ($use["ru_stime.tv_usec"] / 1000) - $t;
             $UtilsDtStats[$ndx][] = $dt;
             return $dt;
          };
          $obj->stats = function(int $ndx = 0) use (&$UtilsDtStats) {
             $s = @$UtilsDtStats[$ndx];
             if($s !== null)
                $s = \array_slice($s, 0);
             else
                $s = false;
             return $s;
          };
          $obj->statsLength = function() use (&$UtilsDtStats) {
             return \count($UtilsDtStats);
          };
          return $obj;
       }
    }
    
    4
    namespace g3;
    
    class Utils {
       public function __construct() {}
    
       public static $UtilsDtStart = [];
       public static $UtilsDtStats = [];
    
       public static function dt() {
          global $UtilsDtStart, $UtilsDtStats;
          $obj = new \stdClass();
          $obj->start = function(int $ndx = 0) use (&$UtilsDtStart) {
             $UtilsDtStart[$ndx] = \microtime(true) * 1000;
          };
          $obj->codeStart = function(int $ndx = 0) use (&$UtilsDtStart) {
             $use = \getrusage();
             $UtilsDtStart[$ndx] = ($use["ru_utime.tv_sec"] * 1000) + ($use["ru_utime.tv_usec"] / 1000);
          };
          $obj->resourceStart = function(int $ndx = 0) use (&$UtilsDtStart) {
             $use = \getrusage();
             $UtilsDtStart[$ndx] = $use["ru_stime.tv_usec"] / 1000;
          };
          $obj->end = function(int $ndx = 0) use (&$UtilsDtStart, &$UtilsDtStats) {
             $t = @$UtilsDtStart[$ndx];
             if($t === null)
                return false;
             $end = \microtime(true) * 1000;
             $dt = $end - $t;
             $UtilsDtStats[$ndx][] = $dt;
             return $dt;
          };
          $obj->codeEnd = function(int $ndx = 0) use (&$UtilsDtStart, &$UtilsDtStats) {
             $t = @$UtilsDtStart[$ndx];
             if($t === null)
                return false;
             $use = \getrusage();
             $dt = ($use["ru_utime.tv_sec"] * 1000) + ($use["ru_utime.tv_usec"] / 1000) - $t;
             $UtilsDtStats[$ndx][] = $dt;
             return $dt;
          };
          $obj->resourceEnd = function(int $ndx = 0) use (&$UtilsDtStart, &$UtilsDtStats) {
             $t = @$UtilsDtStart[$ndx];
             if($t === null)
                return false;
             $use = \getrusage();
             $dt = ($use["ru_stime.tv_usec"] / 1000) - $t;
             $UtilsDtStats[$ndx][] = $dt;
             return $dt;
          };
          $obj->stats = function(int $ndx = 0) use (&$UtilsDtStats) {
             $s = @$UtilsDtStats[$ndx];
             if($s !== null)
                $s = \array_slice($s, 0);
             else
                $s = false;
             return $s;
          };
          $obj->statsLength = function() use (&$UtilsDtStats) {
             return \count($UtilsDtStats);
          };
          return $obj;
       }
    }
    
    5
    namespace g3;
    
    class Utils {
       public function __construct() {}
    
       public static $UtilsDtStart = [];
       public static $UtilsDtStats = [];
    
       public static function dt() {
          global $UtilsDtStart, $UtilsDtStats;
          $obj = new \stdClass();
          $obj->start = function(int $ndx = 0) use (&$UtilsDtStart) {
             $UtilsDtStart[$ndx] = \microtime(true) * 1000;
          };
          $obj->codeStart = function(int $ndx = 0) use (&$UtilsDtStart) {
             $use = \getrusage();
             $UtilsDtStart[$ndx] = ($use["ru_utime.tv_sec"] * 1000) + ($use["ru_utime.tv_usec"] / 1000);
          };
          $obj->resourceStart = function(int $ndx = 0) use (&$UtilsDtStart) {
             $use = \getrusage();
             $UtilsDtStart[$ndx] = $use["ru_stime.tv_usec"] / 1000;
          };
          $obj->end = function(int $ndx = 0) use (&$UtilsDtStart, &$UtilsDtStats) {
             $t = @$UtilsDtStart[$ndx];
             if($t === null)
                return false;
             $end = \microtime(true) * 1000;
             $dt = $end - $t;
             $UtilsDtStats[$ndx][] = $dt;
             return $dt;
          };
          $obj->codeEnd = function(int $ndx = 0) use (&$UtilsDtStart, &$UtilsDtStats) {
             $t = @$UtilsDtStart[$ndx];
             if($t === null)
                return false;
             $use = \getrusage();
             $dt = ($use["ru_utime.tv_sec"] * 1000) + ($use["ru_utime.tv_usec"] / 1000) - $t;
             $UtilsDtStats[$ndx][] = $dt;
             return $dt;
          };
          $obj->resourceEnd = function(int $ndx = 0) use (&$UtilsDtStart, &$UtilsDtStats) {
             $t = @$UtilsDtStart[$ndx];
             if($t === null)
                return false;
             $use = \getrusage();
             $dt = ($use["ru_stime.tv_usec"] / 1000) - $t;
             $UtilsDtStats[$ndx][] = $dt;
             return $dt;
          };
          $obj->stats = function(int $ndx = 0) use (&$UtilsDtStats) {
             $s = @$UtilsDtStats[$ndx];
             if($s !== null)
                $s = \array_slice($s, 0);
             else
                $s = false;
             return $s;
          };
          $obj->statsLength = function() use (&$UtilsDtStats) {
             return \count($UtilsDtStats);
          };
          return $obj;
       }
    }
    
    6
    namespace g3;
    
    class Utils {
       public function __construct() {}
    
       public static $UtilsDtStart = [];
       public static $UtilsDtStats = [];
    
       public static function dt() {
          global $UtilsDtStart, $UtilsDtStats;
          $obj = new \stdClass();
          $obj->start = function(int $ndx = 0) use (&$UtilsDtStart) {
             $UtilsDtStart[$ndx] = \microtime(true) * 1000;
          };
          $obj->codeStart = function(int $ndx = 0) use (&$UtilsDtStart) {
             $use = \getrusage();
             $UtilsDtStart[$ndx] = ($use["ru_utime.tv_sec"] * 1000) + ($use["ru_utime.tv_usec"] / 1000);
          };
          $obj->resourceStart = function(int $ndx = 0) use (&$UtilsDtStart) {
             $use = \getrusage();
             $UtilsDtStart[$ndx] = $use["ru_stime.tv_usec"] / 1000;
          };
          $obj->end = function(int $ndx = 0) use (&$UtilsDtStart, &$UtilsDtStats) {
             $t = @$UtilsDtStart[$ndx];
             if($t === null)
                return false;
             $end = \microtime(true) * 1000;
             $dt = $end - $t;
             $UtilsDtStats[$ndx][] = $dt;
             return $dt;
          };
          $obj->codeEnd = function(int $ndx = 0) use (&$UtilsDtStart, &$UtilsDtStats) {
             $t = @$UtilsDtStart[$ndx];
             if($t === null)
                return false;
             $use = \getrusage();
             $dt = ($use["ru_utime.tv_sec"] * 1000) + ($use["ru_utime.tv_usec"] / 1000) - $t;
             $UtilsDtStats[$ndx][] = $dt;
             return $dt;
          };
          $obj->resourceEnd = function(int $ndx = 0) use (&$UtilsDtStart, &$UtilsDtStats) {
             $t = @$UtilsDtStart[$ndx];
             if($t === null)
                return false;
             $use = \getrusage();
             $dt = ($use["ru_stime.tv_usec"] / 1000) - $t;
             $UtilsDtStats[$ndx][] = $dt;
             return $dt;
          };
          $obj->stats = function(int $ndx = 0) use (&$UtilsDtStats) {
             $s = @$UtilsDtStats[$ndx];
             if($s !== null)
                $s = \array_slice($s, 0);
             else
                $s = false;
             return $s;
          };
          $obj->statsLength = function() use (&$UtilsDtStats) {
             return \count($UtilsDtStats);
          };
          return $obj;
       }
    }
    
    7

    namespace g3;
    
    class Utils {
       public function __construct() {}
    
       public static $UtilsDtStart = [];
       public static $UtilsDtStats = [];
    
       public static function dt() {
          global $UtilsDtStart, $UtilsDtStats;
          $obj = new \stdClass();
          $obj->start = function(int $ndx = 0) use (&$UtilsDtStart) {
             $UtilsDtStart[$ndx] = \microtime(true) * 1000;
          };
          $obj->codeStart = function(int $ndx = 0) use (&$UtilsDtStart) {
             $use = \getrusage();
             $UtilsDtStart[$ndx] = ($use["ru_utime.tv_sec"] * 1000) + ($use["ru_utime.tv_usec"] / 1000);
          };
          $obj->resourceStart = function(int $ndx = 0) use (&$UtilsDtStart) {
             $use = \getrusage();
             $UtilsDtStart[$ndx] = $use["ru_stime.tv_usec"] / 1000;
          };
          $obj->end = function(int $ndx = 0) use (&$UtilsDtStart, &$UtilsDtStats) {
             $t = @$UtilsDtStart[$ndx];
             if($t === null)
                return false;
             $end = \microtime(true) * 1000;
             $dt = $end - $t;
             $UtilsDtStats[$ndx][] = $dt;
             return $dt;
          };
          $obj->codeEnd = function(int $ndx = 0) use (&$UtilsDtStart, &$UtilsDtStats) {
             $t = @$UtilsDtStart[$ndx];
             if($t === null)
                return false;
             $use = \getrusage();
             $dt = ($use["ru_utime.tv_sec"] * 1000) + ($use["ru_utime.tv_usec"] / 1000) - $t;
             $UtilsDtStats[$ndx][] = $dt;
             return $dt;
          };
          $obj->resourceEnd = function(int $ndx = 0) use (&$UtilsDtStart, &$UtilsDtStats) {
             $t = @$UtilsDtStart[$ndx];
             if($t === null)
                return false;
             $use = \getrusage();
             $dt = ($use["ru_stime.tv_usec"] / 1000) - $t;
             $UtilsDtStats[$ndx][] = $dt;
             return $dt;
          };
          $obj->stats = function(int $ndx = 0) use (&$UtilsDtStats) {
             $s = @$UtilsDtStats[$ndx];
             if($s !== null)
                $s = \array_slice($s, 0);
             else
                $s = false;
             return $s;
          };
          $obj->statsLength = function() use (&$UtilsDtStats) {
             return \count($UtilsDtStats);
          };
          return $obj;
       }
    }
    
    9

    Output:

    Execution time of script = 1.6927719116211E-5 sec
    

    Chỉ để đóng góp cho cuộc trò chuyện này:

    • Điều gì xảy ra nếu phép đo nhắm mục tiêu hai điểm A và B trong các tệp PHP khác nhau?

    • Điều gì sẽ xảy ra nếu chúng ta cần các phép đo khác nhau như thời gian dựa trên thời gian, thời lượng thực hiện mã, thời lượng truy cập tài nguyên bên ngoài?

    • Điều gì sẽ xảy ra nếu chúng ta cần tổ chức các phép đo của mình trong các danh mục mà mỗi người có điểm khởi đầu khác nhau?

    Khi bạn nghi ngờ, chúng tôi cần một số biến toàn cầu được truy cập bởi một đối tượng lớp hoặc phương pháp tĩnh: Tôi chọn cách tiếp cận thứ 2 và đây là:

    namespace g3;
    
    class Utils {
       public function __construct() {}
    
       public static $UtilsDtStart = [];
       public static $UtilsDtStats = [];
    
       public static function dt() {
          global $UtilsDtStart, $UtilsDtStats;
          $obj = new \stdClass();
          $obj->start = function(int $ndx = 0) use (&$UtilsDtStart) {
             $UtilsDtStart[$ndx] = \microtime(true) * 1000;
          };
          $obj->codeStart = function(int $ndx = 0) use (&$UtilsDtStart) {
             $use = \getrusage();
             $UtilsDtStart[$ndx] = ($use["ru_utime.tv_sec"] * 1000) + ($use["ru_utime.tv_usec"] / 1000);
          };
          $obj->resourceStart = function(int $ndx = 0) use (&$UtilsDtStart) {
             $use = \getrusage();
             $UtilsDtStart[$ndx] = $use["ru_stime.tv_usec"] / 1000;
          };
          $obj->end = function(int $ndx = 0) use (&$UtilsDtStart, &$UtilsDtStats) {
             $t = @$UtilsDtStart[$ndx];
             if($t === null)
                return false;
             $end = \microtime(true) * 1000;
             $dt = $end - $t;
             $UtilsDtStats[$ndx][] = $dt;
             return $dt;
          };
          $obj->codeEnd = function(int $ndx = 0) use (&$UtilsDtStart, &$UtilsDtStats) {
             $t = @$UtilsDtStart[$ndx];
             if($t === null)
                return false;
             $use = \getrusage();
             $dt = ($use["ru_utime.tv_sec"] * 1000) + ($use["ru_utime.tv_usec"] / 1000) - $t;
             $UtilsDtStats[$ndx][] = $dt;
             return $dt;
          };
          $obj->resourceEnd = function(int $ndx = 0) use (&$UtilsDtStart, &$UtilsDtStats) {
             $t = @$UtilsDtStart[$ndx];
             if($t === null)
                return false;
             $use = \getrusage();
             $dt = ($use["ru_stime.tv_usec"] / 1000) - $t;
             $UtilsDtStats[$ndx][] = $dt;
             return $dt;
          };
          $obj->stats = function(int $ndx = 0) use (&$UtilsDtStats) {
             $s = @$UtilsDtStats[$ndx];
             if($s !== null)
                $s = \array_slice($s, 0);
             else
                $s = false;
             return $s;
          };
          $obj->statsLength = function() use (&$UtilsDtStats) {
             return \count($UtilsDtStats);
          };
          return $obj;
       }
    }
    

    Bây giờ tất cả những gì bạn có là gọi phương thức thuộc danh mục cụ thể với chỉ mục biểu thị nhóm duy nhất:

    File A
    ------
    \call_user_func_array(\g3\Utils::dt()->start, [0]);   // point A
    ...
    File B
    ------
    $dt = \call_user_func_array(\g3\Utils::dt()->end, [0]);  // point B
    

    Giá trị for4 chứa mili giây của thời gian đồng hồ tường giữa các điểm A và B.

    Để ước tính thời gian để mã PHP chạy:

    File A
    ------
    \call_user_func_array(\g3\Utils::dt()->codeStart, [1]);   // point A
    ...
    File B
    ------
    $dt = \call_user_func_array(\g3\Utils::dt()->codeEnd, [1]);  // point B
    

    Lưu ý cách chúng tôi thay đổi chỉ mục mà chúng tôi vượt qua các phương pháp.

    Mã này dựa trên hiệu ứng đóng xảy ra khi chúng ta trả về một đối tượng/hàm từ hàm (xem for5 được lặp lại tại các ví dụ).

    Tôi đã thử nghiệm với đơn vị PHP và giữa các phương pháp kiểm tra khác nhau ở cùng một tệp kiểm tra, nó hoạt động tốt cho đến nay!

    Hy vọng điều đó sẽ giúp ai đó!

    Php tính toán thời gian thực hiện như thế nào?

    Thời gian đồng hồ có thể sử dụng hàm microtime (). Đầu tiên sử dụng nó trước khi bắt đầu tập lệnh và sau đó ở cuối tập lệnh. Sau đó sử dụng công thức (end_time - start_time). Hàm microtime () trả về thời gian tính bằng giây.using microtime() function. First use it before starts the script and then at the end of the script. Then using formula (End_time – Start_time). The microtime() function returns time in seconds.

    Thời gian thực hiện tối đa là gì?

    Thời gian thực hiện tối đa (max_execut_time) là giới hạn thời gian về thời gian một tập lệnh PHP có thể chạy.Đó là một cách mà các nhà cung cấp lưu trữ có thể giới hạn việc sử dụng và lạm dụng tài nguyên máy chủ, đặc biệt là đối với lưu trữ được chia sẻ.Giá trị mặc định thực tế phụ thuộc vào lưu trữ, nhưng nó thường được đặt thành 30 (nghĩa là 30 giây).a time limit on how long a PHP script can run. It is a way hosting providers can limit the use and abuse of server resources, especially for shared hosting. The actual default value depends on the hosting, but it-s usually set to 30 (i.e. 30 seconds).

    Thời gian thực hiện tối đa mặc định trong PHP là gì?

    Theo mặc định, thời gian thực hiện tối đa cho các tập lệnh PHP được đặt thành 30 giây.Nếu một tập lệnh chạy dài hơn 30 giây, PHP sẽ dừng tập lệnh và báo cáo lỗi.Bạn có thể kiểm soát lượng thời gian PHP cho phép các tập lệnh chạy bằng cách thay đổi Chỉ thị MAX_EXECUTY_TIME trong PHP của bạn.Tệp INI.30 seconds. If a script runs for longer than 30 seconds, PHP stops the script and reports an error. You can control the amount of time PHP allows scripts to run by changing the max_execution_time directive in your php. ini file.

    PHP có thời gian chờ không?

    Thời gian chờ ổ cắm mặc định trong PHP là 60 giây.Các yêu cầu HTTP được thực hiện với ví dụ File_Get_Contents, Fopen, SoapClient hoặc DomDocument :: Load đang sử dụng cài đặt thời gian chờ này để quyết định thời gian chờ đợi phản hồi.. HTTP requests performed with for example file_get_contents , fopen , SOAPClient or DOMDocument::load are using this timeout INI setting to decide how long to wait for a response.