Timer page running time monitoring class implemented in php, phptimer_PHP tutorial

WBOY
Release: 2016-07-13 10:18:17
Original
1143 people have browsed it

Timer page running time monitoring class implemented by php, phptimer

The example in this article describes the Timer page running time monitoring class and its usage implemented in PHP. It is a very practical PHP class file. Share it with everyone for your reference. The specific analysis is as follows:

This php Timer page running time monitoring class can monitor different running times according to different keys.

Timer.class.php class file is as follows:

<&#63;php 
/** Timer class, 计算页面运行时间,可按不同key计算不同的运行时间 
*  Date:  2014-02-28 
*  Author: fdipzone 
*  Ver:  1.0 
* 
*  Func: 
*  public start    记录开始时间 
*  public end     记录结束时间 
*  public getTime   计算运行时间 
*  pulbic printTime  输出运行时间 
*  private getKey    获取key 
*  private getMicrotime 获取microtime 
*/ 
 
class Timer{ // class start 
 
  private $_start = array(); 
  private $_end = array(); 
  private $_default_key = 'Timer'; 
  private $_prefix = 'Timer_'; 
 
  /** 记录开始时间 
  * @param String $key 标记 
  */ 
  public function start($key=''){ 
    $flag = $this->getKey($key); 
    $this->_start[$flag] = $this->getMicrotime(); 
  } 
 
  /** 记录结束时间 
  * @param String $key 标记 
  */ 
  public function end($key=''){ 
    $flag = $this->getKey($key); 
    $this->_end[$flag] = $this->getMicrotime(); 
  } 
 
  /** 计算运行时间 
  * @param String $key 标记 
  * @return float 
  */ 
  public function getTime($key=''){ 
    $flag = $this->getKey($key); 
    if(isset($this->_end[$flag]) && isset($this->_start[$flag])){ 
      return (float)($this->_end[$flag] - $this->_start[$flag]); 
    }else{ 
      return 0; 
    } 
  } 
 
  /** 输出页面运行时间 
  * @param String $key 标记 
  * @return String 
  */ 
  public function printTime($key=''){ 
    printf("%srun time %f ms\r\n", $key==''&#63; $key : $key.' ', $this->getTime($key)*1000); 
  } 
 
  /** 获取key 
  * @param String $key 标记 
  * @return String 
  */ 
  private function getKey($key=''){ 
    if($key==''){ 
      return $this->_default_key; 
    }else{ 
      return $this->_prefix.$key; 
    } 
  } 
 
  /** 获取microtime 
  */ 
  private function getMicrotime(){ 
    list($usec, $sec) = explode(' ', microtime()); 
    return (float)$usec + (float)$sec; 
  } 
} // class end 
&#63;>

Copy after login

The demo sample code is as follows:

<&#63;php 
 
require 'Timer.class.php'; 
 
$timer = new Timer(); 
$timer->start(); 
 
$timer->start('program1'); 
usleep(mt_rand(100000,500000)); 
$timer->end('program1'); 
$timer->printTime('program1'); 
 
$timer->start('program2'); 
usleep(mt_rand(100000,500000)); 
$timer->end('program2'); 
$timer->printTime('program2'); 
 
$timer->end(); 
$timer->printTime(); 
 
&#63;>

Copy after login

Demo running output:

program1 run time 163.285971 ms 
program2 run time 100.347042 ms 
run time 264.035940 ms 

Copy after login

Click here to download the complete example source code.

I hope this article will be helpful to everyone’s PHP programming design.

I want to use php to set the time to: 00:00 tomorrow, how to achieve it? Just get the current time first, starting from the current time: 0:0

First of all
The answer on the first floor is wrong. In fact, php has nothing to do with web pages.
The operation of "opening a web page" is just to send information to the server, and then the server executes php.
PHP can be executed directly as a script on the shell, or it can be started with a web page and then closed and the web page will still be executed.

Then answer the poster’s question

Your request, but the program will be closed when it reaches 24:00?
There is no need to record the startup time at all, as long as it reaches 24 o'clock, it can be shut down.
Write an infinite loop
ignore_user_abort(); // Allow php to run in the background
set_time_limit(0); // Cancel the response time of php

do{
$time=date('h:i');//Get the current time
if($time=="24:00")//Exit the loop when the time is reached
break;
sleep(1000); // Run once every second
}while(true);
?>
Modify this program to achieve the effect you want. If your requirement is that you must exit at 24:00 the next day, then please write the date as date('Y-M-D h:i')
and then use the split method to disassemble and calculate the date

How to use timer in PHP?

It’s indeed a very unclear question.
The reason for your error is that the referenced file does not exist and this class cannot be called.
My QQ 42696883

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/883682.htmlTechArticleTimer page running time monitoring class implemented by php, phptimer This article describes the Timer page running time monitoring class implemented by php and its usage, it is a very practical PHP class file. Share to...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!