Detailed explanation of the php microtime() function to calculate the running time of page scripts

怪我咯
Release: 2023-03-07 14:44:01
Original
3134 people have browsed it

Usually when we browse the website, we often use the search function. When searching for information, careful users will find that at the bottom of the search results, there is usually the words "search time is x seconds". This is using PHP'smicrotime() function.

The microtime() function returns the current UNIX timestamp and microseconds. Returns a string in the format mesc sec, where sec is the current UNIX timestamp and msec is the number of microseconds.

The syntax is as follows

microtime(get_as_float);
Copy after login

Parameter description

When set to TRUE, it specifies that the function should return a floating point number, otherwise it returns a string. Default is FALSE.

Example

Let's calculate the running time of a piece of code. First, declare a function run_time(), which returns the current time, accurate to the microsecond. Run the function once before running the PHP code segment, save the return value to the variable

$start_time, and then run the PHP code segment. When the code segment is finished running, call the run_time() function again and save the return value to the variable $end_time. The difference between the two variables is the running time of the PHP code segment.

The sample code is as follows:

"; echo "离端午节放假时间还有"." ".$sub2." "."天!!!"; $end_time=run_time(); $time=$end_time-$start_time; //再次运行代码段 echo "

"; echo "该示例的运行时间为".$time."秒"; ?>

Copy after login

Detailed explanation of the example:

explode function, the function format is

explode(separator,string,limit)
Copy after login

The function of this function is to convert the string according to the specified String or character (separator) cut, if the separator is empty (""), then the function will return false; if the value contained in the separator is not found in the string, then the function will return an array of single elements of the string .

list() function, the function format is:

list(var1,var2...)
Copy after login

The function of this function is to assign the values in the array to some variables var1.

The sample running results are as follows:

Detailed explanation of the php microtime() function to calculate the running time of page scripts

Summary

Through the previous series of studies, We introduce the commonly used functions for processing date and time in PHP, which are mainly divided into three aspects: first, it introduces thesystem time zone settings, and then introduces the built-in date and time functions in PHP. There areLocal timestamp mktime() function,Get the current timestamp time() function,Get the current date and time date() function,Get date information getdate() function,Check the validity of the date checkdate() function,Parse the date and time into a UNIX timestamp strtotime() function, and finally introduce the date and time Commonly used methodsUse the strtotime() function to compare the size of two times,Use the PHP time and date function strtotime() to implement the countdown function detailed exampleAnd the calculation page script explained in this chapter The running time, etc. I hope that through this series of studies, everyone can skillfully use the date and time functions in PHP.

The above is the detailed content of Detailed explanation of the php microtime() function to calculate the running time of page scripts. For more information, please follow other related articles on the PHP Chinese website!

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
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!