InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

Menu
  • Home
  • Sitemap

Python Programming Language Best Tutorials and Code Examples

Learn Python Right Now!
Home
PHP
Generating a High-Precision Time in PHP
PHP

Generating a High-Precision Time in PHP

InfinityCoder November 18, 2016

You need to measure time with finer than one-second resolution—for example, to generate a unique ID or benchmark a function call.

Use microtime(true) to get the current time in seconds and microseconds. Example 3-24 uses microtime(true) to time how long it takes to do 1,000 regular expression matches.

Example 3-24. Timing with microtime()

1
2
3
4
5
6
$start = microtime(true);
for ($i = 0; $i < 1000; $i++) {
     preg_match('/age=\d{1,5}/',$_SERVER['QUERY_STRING']);
}
$end = microtime(true);
$elapsed = $end - $start;

Without an argument that evaluates to true, microtime() returns a string that contains the microseconds part of elapsed time since the epoch, a space, and seconds since the epoch.

For example, a return value of 0.41644100 1026683258 means that 1026683258.41644100 seconds have elapsed since the epoch.

This allows for more precision than can fit into a float, but makes it difficult to calculate with.

Since PHP 5.4.0, the $_SERVER superglobal array is populated with a RE QUEST_TIME_FLOAT entry. This contains the time (including microseconds) when the request started.

This makes it easy to determine how long a request has been running at any point—just compute microtime(true) – $_SERVER[‘REQUEST_TIME_FLOAT’].

Time including microseconds is useful for generating unique IDs. When combined with the current process ID, it guarantees a unique ID, as long as a process doesn’t generate more than one ID per microsecond.

Example 3-25 uses microtime() (with its string return format) to generate just such an ID.

Example 3-25. Generating an ID with microtime()

1
2
list($microseconds,$seconds) = explode(' ',microtime());
$id = $seconds.$microseconds.getmypid();

Note that the method in Example 3-25 is not as foolproof on multithreaded systems, where there is a nonzero (but very tiny) chance that two threads of the same process could call microtime() during the same microsecond.

Share
Tweet
Email
Prev Article
Next Article

Related Articles

Finding the Current Date and Time in PHP
You want to know what the time or date is. …

Finding the Current Date and Time in PHP

Performing DNS Lookups in PHP
You want to look up a domain name or an …

Performing DNS Lookups in PHP

About The Author

InfinityCoder
InfinityCoder

Leave a Reply

Cancel reply

Recent Tutorials InfinityQuest

  • Adding New Features to bash Using Loadable Built-ins in bash
    Adding New Features to bash Using Loadable …
    June 27, 2017 0
  • Getting to the Bottom of Things in bash
    Getting to the Bottom of Things in …
    June 27, 2017 0

Recent Comments

  • fer on Turning a Dictionary into XML in Python
  • mahesh on Turning a Dictionary into XML in Python

Categories

  • Bash
  • PHP
  • Python
  • Uncategorized

InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

About Us

Start learning your desired programming language with InfinityQuest.com.

On our website you can access any tutorial that you want with video and code examples.

We are very happy and honored that InfinityQuest.com has been listed as a recommended learning website for students.

Popular Tags

binary data python CIDR convert string into datetime python create xml from dict python dictionary into xml python how to create xml with dict in Python how to write binary data in Python IP Address read binary data python tutorial string as date object python string to datetime python

Archives

  • June 2017
  • April 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
Copyright © 2021 InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more
Programming Tutorials | Sitemap