Example of generating high precision time in php
- $ Start = microtime (true );
- For ($ I = 0; I I <1000; $ I ++ ){
- Preg_match ('/age = \ d +/', $ _ SERVER ['query _ string']);
- }
- $ End = microtime (true );
- ?>
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
- List ($ microseconds, $ seconds) = explode ('', microtime ());
- $ Id = $ seconds. $ microseconds. getmypid ();
- ?>
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 (). |