Use subtle calculations for PHP script execution time

Source: Internet
Author: User

In PHP, most of the time formats are expressed in Unix timestamps, while the UNIX timestamp is the unit of time measured in s (seconds). This is not accurate enough for some applications, so you can call Microtime () to return the current UNIX timestamp and the number of subtleties. The function is prototyped as follows:

Mixed Microtime ([bool get_as_float]); Returns the current UNIX timestamp and the number of subtleties

You can provide an optional Boolean parameter for the function, and if this argument is not provided at the time of invocation, the function returns a string in the format "msec sec". Where the SEC is the number of seconds from the Unix era to the present, and msec is a subtle part, the two parts of the string are returned in seconds. If the Get_as_float parameter is given and its value is equivalent to True,microtime (), a floating-point number is returned. Before the decimal point or in timestamp format, and after the decimal point represents a subtle value. Note, however, that the parameter get_as_float is new in the PHP5.0 version, so in the previous version of PHP5, you cannot directly request a floating-point number using this parameter directly. In the following example, the time required to run a PHP script is calculated by calling the Microtime () function two times. The code looks like this:

1234567891011121314151617181920212223242526272829 <?php//生命一个计算脚本运行时间的类class Timer{private $startTime = 0; //保存脚本开始执行时的时间(以微秒的形式保存)private $stopTime = 0; //保存脚本结束执行时的时间(以微秒的形式保存)//在脚本开始处调用获取脚本开始时间的微秒值function start(){$this->startTime = microtime(true); //将获取的时间赋值给成员属性$startTime}//脚本结束处嗲用脚本结束的时间微秒值function stop(){$this->stopTime = microtime(true); //将获取的时间赋给成员属性$stopTime}//返回同一脚本中两次获取时间的差值function spent(){//计算后4舍5入保留4位返回return round(($this->stopTime-$this->startTime),4);}}$timer= new Timer(); $timer->start(); //在脚本文件开始执行时调用这个方法usleep(1000); //脚本的主题内容,这里可以休眠一毫秒为例$timer->stop(); //在脚本文件结束处调用这个方法echo "执行该脚本用时<b>".$timer->spent()."</b>";?>

In the above script, declare a class timer that is used to calculate the execution time of the script. You need to call the start () method in the class at the beginning of the script execution to get the time when the script starts executing. and call the Stop () method in the class at the end of the script execution to get the time at the end of the script run. By accessing the spent () method in the class, you can get the time it takes to run the script.

>> This article fixed link: http://php.ncong.com/php_course/date/weimiaojisuan.html

>> reprint Please specify: Ntshongwana PHP August 08, 2014 Yuncon PHP Learning tutorial Published

Related Article

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.