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);
?>
Related
i am new to stackOverflow, i was wondering how could i get the missing time date to a microtime(true) starting from another microtime(true), and get it formatted like that (H:i:s).
This is my code:
$rewardCountdown = microtime(true) - $this->dailyRewardTime; // daily reward is another microtime(true)
$rewardAvailable = $rewardCountdown >= 60 * 60 * 24; // check if 24h are gone so we can get reward
Basically i want to get the $rewardCountdown in this format (H:i:s)
I tried and somehow got something like that but i was getting the time increasing instead of decreasing
The current microtime minus a previous microtime would be a positive value, of the number of seconds passed between the two times.
If you want to convert seconds into Hours, Minutes and Seconds, you can simply use gmdate.
$rewardCountdown = microtime(true) - $this->dailyRewardTime;
$date = gmdate('H:i:s', $rewardCountdown);
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');
I need a php code that when you type (time), and a (base number), it increases by base number value, on specific time intervals.
e.g:
every 40 minutes, add 0.0001 to the base number 0.04
0.04
0.0401
0.0402
.....
You can use the unixtime and calculate how many 40 minutes that has elapsed.
$start = 1537088883; //Unix time of when I started writing answer.
$elapsed = time() - $start; // elapsed seconds since start
$counts = floor($elapsed/(60*40)); // number of 40 minutes that has elapsed
echo 0.04 + (0.0001*$counts);
https://3v4l.org/q9Vhm
Right now it will output just 0.04, but if we manipulate the $start we will get a different output ( or just wait 40 minutes and the same code will output a different number).
https://3v4l.org/E9XWN
If you want to set a start date and time you can use strtotime to convert a human readable date to Unix time.
$start = strtotime("2018-09-01 09:00:00");
https://3v4l.org/c6g0l
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
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.