PHP get exact time a particular line is executed - php

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

Related

Getting time difference in micro or milli seconds in php

I am trying to find time required to execute some function using php.
I want accuracy of milli or micro seconds but i get difference in seconds.
The code is as follows:
<?php
$time_start = microtime(true);
usleep(2000000);
$time_end = microtime(true);
$time = $time_end - $time_start;
echo "Did nothing in $time micro seconds\n";
?>
The output i get is 2 micro seconds but it should be 2000000 micro seconds according to program.
If i keep usleep(100) it will give me 0 difference.
Please help me with some solution to this.
The result of microsecond(true) is a float, "which represents the current time in seconds" and "accurate to the nearest microsecond" (http://php.net/microtime). Meaning decimals before the comma are in seconds and the decimals after the comma represent the microseconds.
Therefore the example in the manual is more correct than your variant:
echo "Did nothing in $time seconds\n";
If the execution takes exactly two seconds, it will output:
Did nothing in 2 seconds
If the execution takes two seconds and 500 milliseconds, it will output:
Did nothing in 2.5 seconds
use this code
<?php
function secondsConverter($dateTime1,$dateTime2)
{
$hour=abs($dateTime1['hour']-$dateTime2['hour'])*3600;
$minutes=abs($dateTime1['minute']-$dateTime2['minute'])*60;
$seconds=abs($dateTime1['second']-$dateTime2['second']);
return $hour+$minutes+$seconds;
}
$dateTime1=date_parse("10:00:00.5");
$dateTime2=date_parse("11:00:00.5");
echo secondsConverter($dateTime1,$dateTime2);
?>

php set_time_limit less than one second

The question is simple, I would like to check a database to serve customised content to a site visitor, but failover and serve a generic page if this function takes more then 800ms to execute. (Target time for the server response is 1000ms).
I've seen the set_time_limit function, however this takes an integer in seconds as the argument.
My question: is there something similar that can be used with values of less than 1 second?
I'm looking for something like:
void set_time_limit_ms ( int $milliseconds )
set_time_limit_ms (800)
doesn't exist. you just could emulate it with a tick function:
declare(ticks=1); // or more if 1 takes too much time
$start = microtime(1);
register_tick_function(function () use ($start) {
(microtime(1) - $start < 0.8) or die();
});
You will not be able to use this function to prevent a query that is running longer than you expected. This only measures the actual script execution time. Here is an bit from the manual.
The set_time_limit() function and the configuration directive
max_execution_time only affect the execution time of the script
itself. Any time spent on activity that happens outside the execution
of the script such as system calls using system(), stream operations,
database queries, etc. is not included when determining the maximum
time that the script has been running. This is not true on Windows
where the measured time is real.
Yes there is, I often use microtime. see
http://php.net/manual/en/function.microtime.php
<?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";
?>

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.
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";

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 - 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.

Categories