Guest User

Untitled

a guest
Jan 22nd, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.88 KB | None | 0 0
  1. <?php
  2.  
  3. class Run_Time_Test{
  4.    
  5.     /* 时间控制参数 */
  6.     const MICROSECONDS = 100000;
  7.    
  8.     /* 换行符 */
  9.     //web换行
  10.     const WEB_NEW_LINE = "<br/>";
  11.     //linux换行
  12.     const LINUX_NEW_LINE = "\n";
  13.    
  14.     /* 首次运行时间 */
  15.     public static $first;
  16.    
  17.     /* 上次运行时间 */
  18.     public static $prev;
  19.    
  20.     /* 运行输出字符串 */
  21.     public static $str;
  22.    
  23.     /* 运行环境 */
  24.     public static $environment;
  25.    
  26.     public static $outputBoolean;
  27.    
  28.     /**
  29.      *
  30.      * 时间测试开始
  31.      * @param boolean $environment(false为页面,true为linux环境)
  32.      */
  33.     public static function begin($output=false, $environment=false){
  34.         self::$prev = self::$first = self::getCurrentTime()+0; 
  35.         self::$environment = $environment ? self::LINUX_NEW_LINE : self::WEB_NEW_LINE;
  36.         self::$outputBoolean = $output;
  37.     }
  38.    
  39.     /**
  40.      *
  41.      * 显示上次运行时间
  42.      */
  43.     public static function preTime(){
  44.         /* 获取当前时间 */
  45.         $currentTime = self::getCurrentTime();
  46.         self::setTimeStr($currentTime);  
  47.         self::$prev = $currentTime;
  48.     }
  49.    
  50.     /**
  51.      *
  52.      * 显示总运行时间
  53.      */
  54.     public static function end(){
  55.         $currentTime = self::getCurrentTime();
  56.         self::setTimeStr($currentTime);    
  57.     }
  58.    
  59.     /**
  60.      *
  61.      * 设置时间执行字符串
  62.      */
  63.     protected static function setTimeStr($currentTime){
  64.         $preConsumeTime = intval(($currentTime - self::$prev) * self::MICROSECONDS)/self::MICROSECONDS;
  65.         $allConsumeTime = intval(($currentTime - self::$first) * self::MICROSECONDS)/self::MICROSECONDS;
  66.         self::$str = 'previous time:'. $preConsumeTime . ' | ' . 'all time:'. $allConsumeTime . self::$environment;
  67.         if(self::$outputBoolean){
  68.             echo self::$str;
  69.         }
  70.     }
  71.    
  72.     /**
  73.      *
  74.      * 获取当前时间
  75.      */
  76.     private static function getCurrentTime(){
  77.         $time = explode(' ', microtime());    
  78.         return $time[1] . substr($time[0], 1, 7); // 1212466268.034119形式
  79.     }
  80. }
Add Comment
Please, Sign In to add comment