Example of generating high precision time in php

Source: Internet
Author: User
Example of generating high precision time in php

  1. $ Start = microtime (true );
  2. For ($ I = 0; I I <1000; $ I ++ ){
  3. Preg_match ('/age = \ d +/', $ _ SERVER ['query _ string']);
  4. }
  5. $ End = microtime (true );
  6. ?>

Added support for using optional parameters in microtime () in php 5.0.0. If the parameter is not included, or the parameter cannot be converted to true, or in an earlier version of php, microtime () returns the microsecond of the time elapsed since the Unix epoch, A space and the number of seconds since the epoch. For example, the returned value "0.41644100 1026683258" means that the time elapsed since the epoch is 1026683258.41644100 seconds.

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

Let's take another example: this ID is generated using microtime () (returned string format.

Example 2: Use microtime () to generate an ID

  1. List ($ microseconds, $ seconds) = explode ('', microtime ());
  2. $ Id = $ seconds. $ microseconds. getmypid ();
  3. ?>

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

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.