August, 2009 | Chris Page

PHP Microtime Tutorial

Aug 28

Every once in awhile one has to write a script that is concerned with execution time. Consider the following snippet that utilizes epoch seconds to calculate execution time:

$start_time = microtime(true);
usleep(200000);  // sleep 2 seconds
$end_time = microtime(true);

echo 'Execution time:  ' . ($end_time - $start_time) . "\n";

Pretty straight forward and has many useful applications. By using built in php functions, `microtime` and `usleep`, it’s easy to benchmark operations, and even set time limits on operations. Consider the next snippet which will halt execution after 30 seconds:

$max_execution_time = 30;
$start_time = microtime(true);

while (1) {
    if ((microtime(true) - $start_time) > $max_execution_time)) {

    //Do something

One more clever application, is to pace your scripts. Such may be useful when concerned with load:

while (1) {
     usleep(10000);  // sleep 1/10th of a second
    //Send a request... query a DB... do something

You can do quite a bit with these two functions. It never hurts to read up on microtime, and usleep. Enjoy!

Exciting Updates! Contrary to popular belief, Pittsburgh Rocks, not Cleveland – Period.

Aug 07
Lil Wayne In Pittsburgh

Lil Wayne loves Pitt too!

Two weeks down, this blog is due for an update – time has been flying!

As far as home life in Pittsburgh goes – ROCK. I’m currently staying roughly between one of Carnegie Mellon and Pittsburgh University’s campus intersections – the architecture is gorgeous. I enjoyed a meal at the famous Primanti Brothers down the street, learned the public transit ( a work in progress… ), and discovered a few open mic hot spots I’ll be hitting up eventually – oh, and Lil Wayne at the Post Gazette Pavilion was also ROCK! I’ve had numerous other experiences and visited quite a few restaurants, but won’t detail all my eating habits here 🙂

As far as Lockerz goes… ROCK ON! I’ve made some new friends, thoroughly enjoyed many philosophical discussions, and am getting knee deep into working on the next best thing. Today was a moment for Lockerz history – as we saw an incredible traffic increase this morning – ranking 4 on Google Trends around 10 a.m.

The boost seemed grass roots in nature, as people are catching wind and telling their friends, who are telling their friends. The most personable for me was Amber’s friend made a comment about needing a Lockerz invite to join the site, not even realizing I work there. Small world? Needless to say, he got the invite.

I won’t detail much else about the job – you’ll have to keep tabs on the site! Need an invite? Let me know.