The time that it takes for a function to run - php

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.

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

See "Example #1 Timing script execution with microtime()" in the documentation.
http://docs.php.net/manual/en/function.microtime.php

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

Related

How to read date_time_set as current time in php?

How to read date_time_set as current time in php? it means when i run after 5 min it time should be 3:10
$date=date_create("2018-09-26");
date_time_set($date,3,5);
echo date_format($date,'y-m-d H:i:s')."\n";
//output 18-09-26 03:05:00.
Date objects don't really work that way. They're just for storing time values, they're not a clock. You're going to have to get the time at start via something like $start = time();. From there whenever you want to find out the elapsed time you could do a $elapsed = time() - $start; to get the number of seconds that have passed since the script started and then you're free to do the math from there or do something akin to $date->add($elapsed . ' seconds');

How to find out the time spent inside a PHP script?

I'm executing a PHP script that takes about a minute to finish and although the default time limit is set to 30 seconds, the script continues its execution after that limit.
I found out, that the limit only affects the time that is spent inside the script itself and not the time spent in library functions like database queries etc.
Is there a way to find out the time that is actually spent inside the script? I tried to use getrusage, but it doesn't seem to return the appropriate values for this problem.
Example:
<?php
$time = microtime(TRUE);
sleep(100);
echo 'Time: ', microtime(TRUE) - $time;
?>
The script waits for 100 seconds and does not terminate after the time limit of 30 seconds.
According to the documentation of set_time_limit, the time that is spent inside the sleep function (100 seconds) is not involved in the calculation of the execution time, because it's an external (library) function.
I just want to know how much of the execution time is spent inside my script and how much is spent in library functions.
Use XDebug Profiler for it.
I'm guessing you want something like this:
<?php
// the whole operation
$time1 = time();
// perform some functions
$s = file_get_contents("somefilename");
$time2 = time();
// perform some library functions
$rs = some_third_party_library_function();
$time3 = time();
// Show results
echo "Overall Time spent: " . ($time3 - $time1) . "s <br>";
echo "Time spent reading file: ". ($time2 - $time1) . "s <br>";
echo "Time spent by Third Party Library: " . ($time3 - $time2) . "s <br>";
?>
You can mark the start time before entering the script, then mark the end time after the script ends. Echo the difference then.
<?php
$a=time();
sleep(5);
$b=time();
echo $b-$a;
?>
If you need to measure the execution time of individual functions vs standard or built-in PHP functions, this is best done with a proper debugger and code profiler. Xdebug does exactly that.
Don't know how complicated your script is, however you can just add a comment to all your library functions calls. If your code requires their returned values in order to be properly executed, replace them with constant values (that could have been returned by these functions). For example:
$result = mysql_query( ... )
replace with:
// $result = mysql_query( ... )
$result = // resource, boolean or whatever you want
Then run the script and calculate the execution time using Coding-Freak's suggestion.
If you prefer Rio Bautista's approach you may find some functions that calculates section's time.

PHP get exact time a particular line is executed

I'm trying to perform a simple benchmark on a script I have. First I tried to just add something like:
echo 'After Checklist: '. date('h:i:s:u A') ."<br />";
but it just prints out the same time for a lot of the times - it isn't until a includes script is ran that it returns a different time. Is there anyway to do this? Or something similar - I basically just want to see where the bottleneck is so I can increase performance.
You want to use the microtime function which will return the current Unix timestamp with microseconds.
From here you can do simple subtraction from the start and end time to get the desired results. The trick however, is to pass in "true" into the function so it returns a float value, rather than a string.
An example, as posted on php.net is this:
<?php
$time_start = microtime(true);
// Sleep for a while
usleep(100);
$time_end = microtime(true);
$time = $time_end - $time_start;
echo "Did nothing in $time seconds\n";
?>
I think you need microtime:
http://php.net/manual/en/function.microtime.php

PHP - using microtime() to measure CPU usage of a function/code block

I tried using it like this:
$now = microtime(true);
// cpu expensive code here
echo microtime(true) - $now;
but regardless of what code I enter between these statements, I alwasy get almost the same results, something like 3.0994415283203E-6
What am I doing wrong?
Better solution. Run the code multiple times to average out the operation:
$runs = 500;
$start = microtime(true);
for ($i = 0; $i < $runs; $i++) {
//cpu expensive code here
}
$end = microtime(true);
$elapsed = number_format($end - $start, 4);
$one = number_format(($end - $start) / 500, 7);
echo "500 runs in $elapsed seconds, average of $one seconds per call";
3.0994415283203E-6 equates to 0.0000030994415283203.
The E-6 tells you to move the decimal point left six places. E+6 would mean the opposite. As #deceze mentioned, this is called scientific notation.
If you're doing a performance test, it's a good idea to put the code into a 100000 or so iteration loop, and then divide the resulting time by 100000. That way you get a more accurate average.
You're not doing anything wrong, it's just that the code you're timing really only takes a fraction of a second to run.
If you want to prove it, sleep for a few seconds.
It looks like you are using microtime() without the optional argument, but you say you are, so I am not 100% sure.
What is the output of this:
$now = microtime(true);
sleep(1);
echo microtime(true) - $now;
PHP always amazes me with how fast it is. Your code seems to be right. Maybe your code is really only taking 3 milliseconds.
You could try making a long loop, something like this:
$x=0;
while ($x<1000000)
{
$x++;
}
Add this code inside of your timer. For me, looping 1 million times usually takes about 1/2 second. See if this changes your time.

"Hour:Minute" difference from two "Hour:Minute" strings

Simple, eh? -sigh-
A function. Two string arguments ( I guess it doesn't matter the type). Returning a string which is the time difference between the arguments. Think about it as a counter at your local CS internet-caffe.
function time_diff($start, $stop) {
...
return $diff;
}
echo "Your time playing CS: " . time_diff('19:37', '00:05') . ". Go home!";
I tried. Just too embarrassed to say how much time I've invested in this algorithm.
I can't change the format (tell me about it!).
If you happen to know a class, a file or a piece of code from the depths of the internets, I'd be happy to make use of it.
"Thanks" you very much.
You can't do this correctly unless you know:
The timezone.
The day associated with those times.
Once you know that, you can do:
date_default_timezone_set('timezone here');
$seconds_diff = strtotime("2010-08-29 $end") - strtotime("2010-08-29 $start");

Categories