IMUXIxD
IMUXIxD

Reputation: 1227

The time that it takes for a function to run

What is the best way for getting how long a function takes to run until finish and then storing that number in a variable in PHP ?

How I would think about doing this is get the time right before the function is executed and right after and then take the difference of the former from the latter, but I don't know how get the time in php.

Also, I am trying to get the units to be in tenths and hundredths of second (.42 seconds), hopefully the function takes less than a second to complete so if anyone can help me convert it to those units, i'd appreciate that.

Upvotes: 2

Views: 2837

Answers (3)

spencer7593
spencer7593

Reputation: 108370

See "Example #1 Timing script execution with microtime()" in the documentation.

http://docs.php.net/manual/en/function.microtime.php

Upvotes: 2

kainaw
kainaw

Reputation: 4334

$time = time();
call_your_function();
$time = time()-$time;
echo "The function took $time seconds to run\n";

Upvotes: 0

rationalboss
rationalboss

Reputation: 5389

You can do that using microtime().

$start = microtime(true);
for ($x=0;$x<10000;$x++) {}
$end = microtime(true);
echo 'It took ' . ($end-$start) . ' seconds!';

Upvotes: 16

Related Questions