3

I want to measure how long a specified part of my php script takes, e.g.

$startTime = microtime(true);
//some operations which needs 0.1 to 100 seconds to run
$endTime = microtime(true);

How can I get the duration in microseconds as an integer?

This does not work:

$duration = (int)($endTime - $startTime);
hakre
  • 193,403
  • 52
  • 435
  • 836
Roland
  • 488
  • 1
  • 4
  • 13
  • Why not use `round` ... `$duration = round($endTime - $startTime);` ... it returns a float type, but it will return an integer ... – keithhatfield Mar 20 '12 at 19:53
  • 2
    you are getting the time as an integer, you just have a very basic misunderstanding of what an integer is. anything less that 1 will be set to 0 when you cast it with (int). you probably want something more like $duration = $endTime-$startTime; $duration = number_format($duration,2); – Garry Welding Mar 20 '12 at 19:53
  • 2
    Wait, let's define what you mean by 'microseconds as an integer' ... if the script takes .23 seconds to run are you wanting to see 230? – keithhatfield Mar 20 '12 at 19:58
  • 2
    @dleiftah yes, I need the microseconds (2300) not the seconds – Roland Mar 20 '12 at 19:59

1 Answers1

4

If you want microseconds as an integer:

$duration = round(($endTime - $startTime) * 1000000)
hakre
  • 193,403
  • 52
  • 435
  • 836
keithhatfield
  • 3,273
  • 1
  • 17
  • 24