Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Unique high precision microtime?

Status
Not open for further replies.

RbgoWeb

Programmer
Apr 10, 2002
91
NL
The follwing function returns a microtime(stamp) with 10 digits for the integer,
and most of the time 18 digits sometimes less for the fractional part.
18 digits decimal is nearly a 64 bits number, so a very hi resolution/subdivision
of the second.

function mts()
{
$_p = ini_get('precision');
ini_set('precision', 28);

$mts = (string)microtime(true);

ini_set('precision', $_p);

return $mts;
}

In a single request I can call mts in the tightest possible loop and never get
an equal return value because the resolution is just too high.

But what would the results be in many overlapping requests on the same server,
with multiple processors and or multiple network cards. Is there always some kind
of request sychronisation required before spawning a request handling thread?
In other words, will mts() working in multiple thread always generate unique
values?
 
Thank you OlafDoschke,

I read this url and learned about better building blocks for uniqueness:
[URL unfurl="true"]http://httpd.apache.org/docs/2.2/mod/mod_unique_id.html[/url]

In short I learned:
On one machine, including multiprocessor machines, no two processes that overlap on the timeline can possibly have the same PID (Process ID). So, two overlapping processes may generate the same MTS (microtimestamp) in their overlapping fase, but combining the timestamps with the PID, will garantee uniqueness within the scope of the same machine.

Ofcourse in the same process the precision of the MTS needs to be comfortably high enough to let microtime return different values even if called right after eachother in a very long row.

For uniqueness on global scope, just add the SIP (Server IP) to PID and MTS.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top