Working with microseconds

  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8388
  • Loc: USA

Post 3+ Months Ago

I'm doing the following code to get a generation of time in milliseconds/microseconds... whatever it is.
PHP Code: [ Select ]
$usec = gettimeofday();
$time1 = $usec['usec'];
 
// Code
 
$usec = gettimeofday();
$pageGeneration = $usec['usec'] - $time1;
 
  1. $usec = gettimeofday();
  2. $time1 = $usec['usec'];
  3.  
  4. // Code
  5.  
  6. $usec = gettimeofday();
  7. $pageGeneration = $usec['usec'] - $time1;
  8.  

The problem with that is that I sometimes get a negative number as a result (e.g: -781599 [but mostly I get a positive number, but sometimes get a negative]; which makes me doubt the accuracy of it).

If I try anything else, I'm getting who knows what.

I also tried:
PHP Code: [ Select ]
$start = microtime(true);
 
// Code
 
$total = microtime(true) - $start;
 
  1. $start = microtime(true);
  2.  
  3. // Code
  4.  
  5. $total = microtime(true) - $start;
  6.  

But that gives me something like: 0.18720006942749

Or is that what I'm looking for? Pretty bad when I'm looking at code and don't know if that's what I want :lol: If that is how I want, how do I convert it to milliseconds?

I've also tried the following.
PHP Code: [ Select ]
$start = microtime(true);
 
// Code
 
$time = new DateTime(substr(microtime(true) - $start, 1, 5));
$total = $time->format('u');
 
  1. $start = microtime(true);
  2.  
  3. // Code
  4.  
  5. $time = new DateTime(substr(microtime(true) - $start, 1, 5));
  6. $total = $time->format('u');
  7.  


But that also gives me 000000. Seems like nothing works with microseconds... I don't know what to try at the moment.
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • devilwood
  • Silver Member
  • Silver Member
  • User avatar
  • Posts: 436

Post 3+ Months Ago

Hey, not sure what's going on with ya. The following should be accurate. I would try without floating point numbers just to see. Otherwise, 0.18720006942749 could be the difference between the start and stop. Looks like the script took almost two tenths of second.

Code: [ Select ]

$start = microtime();


//code

$total = microtime() - $start;
  1. $start = microtime();
  2. //code
  3. $total = microtime() - $start;


The float may be why you're getting such a long decimal. If using gettimeofday() is acting screwy then I think you have to look at your server and OS. I would only call gettimeofday if I was needing additional elements from the returned array.

Although, it looks like you're only finding the difference in microseconds. You need the left side of your decimal (ie $usec['sec']). I think usec only returns the the microseconds not the full time.

Post Information

  • Total Posts in this topic: 2 posts
  • Users browsing this forum: No registered users and 65 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
cron
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.