Php implementation of the Timer page runtime monitoring class, phptimer_PHP tutorial

Source: Internet
Author: User
Tags usleep
Phptimer is the runtime monitoring class for Timer pages implemented by php. The Timer 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 file. The Timer page runtime 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 file. Share it with you for your reference. The specific analysis is as follows:

This php Timer page runtime monitoring class can monitor different runtime according to different keys.

The Timer. class. php class file is as follows:

<? Php/** Timer class: calculates the page running time. different running times can be calculated based on different keys * Date: * Author: fdipzone * Ver: 1.0 ** Func: * public start record start time * public end record end time * public getTime calculation run time * pulbic printTime output run time * private getKey get key * private getMicrotime get microtime */class Timer {/ /class start private $ _ start = array (); private $ _ end = array (); private $ _ default_key = 'timer'; private $ _ prefix = 'timer _'; /** Record start time * @ param String $ key mark */public function start ($ key = '') {$ flag = $ this-> getKey ($ key ); $ this-> _ start [$ flag] = $ this-> getMicrotime ();} /** record end time * @ param String $ key mark */public function end ($ key = '') {$ flag = $ this-> getKey ($ key ); $ this-> _ end [$ flag] = $ this-> getMicrotime ();} /** computing run time * @ param String $ key mark * @ 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 ;}} /** output page running time * @ param String $ key mark * @ return String */public function printTime ($ key = '') {printf ("% srun time % f ms \ r \ n", $ key = ''? $ Key: $ key. '', $ this-> getTime ($ key) * 1000 );} /** Get key * @ param String $ key mark * @ return String */private function getKey ($ key = '') {if ($ key = '') {return $ this-> _ default_key;} else {return $ this-> _ prefix. $ key ;}}/** get microtime */private function getMicrotime () {list ($ usec, $ sec) = explode ('', microtime ()); return (float) $ usec + (float) $ sec ;}// class end?>

The demo code is as follows:

<?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();  ?>

Demo Running output:

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

Click here to download the complete instance source code.

I hope this article will help you with PHP programming.


I want to use php timing to: tomorrow, how to implement it? Is to obtain the current time first, at the beginning of the current time: 0: 0

First
The answer on the first floor is incorrect. In fact, php has nothing to do with the webpage.
The "Open Web Page" operation only sends information to the server, and then the server executes php.
Php can be directly executed as a script on shell, or it can be executed after the web page is started.

Then, answer the landlord's question.

What are your requirements, but will the program be closed?
You do not need to record the start time at all. you only need to close it at 24 o'clock.
Write an endless loop
Ignore_user_abort (); // enables php to run in the background
Set_time_limit (0); // cancel the php response time

Do {
$ Time = date ('H: I '); // Obtain the current time
If ($ time = "24:00") // exit the loop when the arrival time is reached.
Break;
Sleep (1000); // runs once per second
} While (true );
?>
Modify the program to achieve the desired effect. If you want to exit at the next day, write date as date ('Y-M-D h: I ')
Split the calculation date

How to use a timer in PHP?

The question is indeed unclear.
The cause of your error is that the referenced file does not exist and this class cannot be called.
My QQ 42696883

The example in this article describes the Timer page runtime monitoring class implemented by php and its usage. it is a very practical PHP file. Share...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.