Example of php generating high-precision time

WBOY
Release: 2016-07-25 09:10:11
Original
1083 people have browsed it
  1. $start = microtime(true);
  2. for($i = 0; $i < 1000; $i++) {
  3. preg_match('/age=d+/',$_SERVER[ 'QUERY_STRING']);
  4. }
  5. $end = microtime(true);
  6. ?>
Copy code

Support for using optional parameters in microtime() was added in php 5.0.0. If taken without arguments, or with arguments that cannot be converted to true, or in earlier versions of PHP, microtime() returns the microsecond portion of the Unix time since the epoch, a space, and the number of seconds since the epoch. number. For example, the return value "0.41644100 1026683258" means that "1026683258.41644100 seconds" have elapsed since the epoch.

A time value containing microseconds is useful for generating unique IDs. As long as a process does not generate more than one ID within a microsecond, it is guaranteed that a time value containing microseconds and the ID of the current process will form a unique ID value.

Let’s look at another example: using microtime() (returned string format) to generate such an ID.

Example 2: Use microtime() to generate an ID

  1. list($microseconds,$seconds) = explode(' ',microtime());
  2. $id = $seconds.$microseconds.getmypid();
  3. ?>
Copy code

Note: Example 2 is not very reliable in a multi-threaded system, because there is a non-trivial (but extremely small) probability that two threads of the same process will call microtime() within the same microsecond.



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!