Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Run_Time_Test{
- /* 时间控制参数 */
- const MICROSECONDS = 100000;
- /* 换行符 */
- //web换行
- const WEB_NEW_LINE = "<br/>";
- //linux换行
- const LINUX_NEW_LINE = "\n";
- /* 首次运行时间 */
- public static $first;
- /* 上次运行时间 */
- public static $prev;
- /* 运行输出字符串 */
- public static $str;
- /* 运行环境 */
- public static $environment;
- public static $outputBoolean;
- /**
- *
- * 时间测试开始
- * @param boolean $environment(false为页面,true为linux环境)
- */
- public static function begin($output=false, $environment=false){
- self::$prev = self::$first = self::getCurrentTime()+0;
- self::$environment = $environment ? self::LINUX_NEW_LINE : self::WEB_NEW_LINE;
- self::$outputBoolean = $output;
- }
- /**
- *
- * 显示上次运行时间
- */
- public static function preTime(){
- /* 获取当前时间 */
- $currentTime = self::getCurrentTime();
- self::setTimeStr($currentTime);
- self::$prev = $currentTime;
- }
- /**
- *
- * 显示总运行时间
- */
- public static function end(){
- $currentTime = self::getCurrentTime();
- self::setTimeStr($currentTime);
- }
- /**
- *
- * 设置时间执行字符串
- */
- protected static function setTimeStr($currentTime){
- $preConsumeTime = intval(($currentTime - self::$prev) * self::MICROSECONDS)/self::MICROSECONDS;
- $allConsumeTime = intval(($currentTime - self::$first) * self::MICROSECONDS)/self::MICROSECONDS;
- self::$str = 'previous time:'. $preConsumeTime . ' | ' . 'all time:'. $allConsumeTime . self::$environment;
- if(self::$outputBoolean){
- echo self::$str;
- }
- }
- /**
- *
- * 获取当前时间
- */
- private static function getCurrentTime(){
- $time = explode(' ', microtime());
- return $time[1] . substr($time[0], 1, 7); // 1212466268.034119形式
- }
- }
Add Comment
Please, Sign In to add comment