php perfomance microtime real value [duplicate] - php

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why is this microtime showing up weird in PHP
I use this code to check performance of the script.
$start_time = microtime(1);
// execution some code
$cpu_time = microtime(1) - $start_time;
the output of echo $cpu_time is something like 3.0994415283203E-6
how to display this to real value like 0.000000003099 on the screen ? Is it 0 seconds and very quick execution, right? :)

Right :)
Try using number_format() function:
echo number_format($cpu_time, 12);

Use number_format.
Like:
number_format($microtime, 15);

Use bcsub
Example
echo bcsub ( microtime ( 1 ), $start_time, 16 );

Related

Question about calculating Average in PHP [duplicate]

This question already has answers here:
How to calculate correctly in php?
(6 answers)
Closed 1 year ago.
I am a newbie learning PHP and i am trying to find average of 3 numbers but not getting the correct answer. I don't know where i am going wrong.
function percentage($math,$eng,$sc){
$s = $math+$eng+$sc / 3 ;
return $s;
}
$p = percentage(10,20,30);
echo $p;
I am getting the ansewer as 40 whereas i am supposed to get 20. Kindly check if there is any error.
Return value is right. Check operators precedence.
If you want 20 as return value code is:
$s = ($math+$eng+$sc) / 3 ;
You forgot to use parentheses:
$s = ($math+$eng+$sc) / 3 ;
All things together:
function percentage($math,$eng,$sc){
$s = ($math+$eng+$sc) / 3 ;
return $s;
}
echo percentage(10,20,30);

reverse chunk split of integer in PHP [duplicate]

This question already has answers here:
Formatting numbers with commas PHP [duplicate]
(4 answers)
Closed 7 months ago.
I need split by gap like when we write count numbers we will give gap for every 3 integer.
echo chunk_split(1000255869,3," ");
I got output like: 100 025 586 9
But I need output like: 1,000,255,869
How to reverse that one?
Try this,
<?php
$a = 1000255869;
echo $a;//output - 1000255869
$a = number_format($a);
echo $a;//output - 1,000,255,869
?>
if you want to do a number format, you should do it with number_format.
echo number_format(1000255869,0,".",",");
This should work number_format(1000255869, 0, '.', ',');
Try this.Reference
$number= 1000255869;
echo number_format($number);

PHP - sleep() in milliseconds [duplicate]

This question already has answers here:
How to pause a script just for a fraction of a second in PHP using the SLEEP() function?
(4 answers)
Closed 3 years ago.
Does PHP provide a function to sleep in milliseconds?
Right now, I'm doing something similar to this, as a workaround.
$ms = 10000;
$seconds = round($ms / 1000, 2);
sleep($seconds);
I'd like to know whether there is a more generic function that is available in PHP to do this, or a better way of handling this.
This is your only pratical alternative: usleep - Delay execution in microseconds
So to sleep for two miliseconds:
usleep( 2 * 1000 );
To sleep for a quater of a second:
usleep( 250000 );
Note that sleep() works with integers, sleep(0.25) would execute as sleep(0) Meaning this function would finish immediatly.
$i = 0;
while( $i < 5000 )
{
sleep(0.25);
echo '.';
$i++;
}
echo 'done';

Invalid value when using floats [duplicate]

This question already has answers here:
PHP - Floating Number Precision [duplicate]
(8 answers)
Closed 9 years ago.
The following operation give out the wrong result.
$First = '45.4000';
$Second = '4.6800000000';
$Third = '50.00';
echo ( $First + $Second ) - $Third;
OUTPUT: 0.079999999999998
Expected Output: 0.08
I am looking on how to get the right result, without using number_format/sprintf ...etc.
As this issue is affecting multiple places in my code & have to go over everything & formatting it is a pain.
As a "quick fix", change the precision setting in your php.ini file. Documentation.
By default, it is 14, which is more than you need almost all the time (and if you need that much precision you'd be using a dedicated math library). Change it to something like 4, and the result will be rounded to that length - note that you can still override this with number_format on a case-by-case basis if you need to.
Try This
$First = '45.4000';
$Second = '4.6800000000';
$Third = '50.00';
$sk = ( $First + $Second ) - $Third;
echo round($sk,4);
?>

How to print rounded percent [duplicate]

This question already has answers here:
Print numeric values to two decimal places
(6 answers)
Closed 11 months ago.
hello i'm trying to find %. first i found the seconds
$tm=sum_the_time($d_duration);
$d_seconds='0';
list($hour,$minute,$second) = explode(':', $tm);
$d_seconds += $hour*3600;
$d_seconds += $minute*60;
$d_seconds += $second;
$total_second=$c_seconds+$p_seconds+$t_seconds+$b_seconds+$d_seconds;
$c_seconds=$c_seconds*100/$total_second;
$p_seconds=$p_seconds*100/$total_second;
$t_seconds=$t_seconds*100/$total_second;
$b_seconds=$b_seconds*100/$total_second;
$d_seconds=$d_seconds*100/$total_second;
echo $c_seconds;
the result is 10.754504504505, how would I print this code like 10.7
You can try using printf() function:
printf("%.1f", $c_seconds);
Or, number_format():
echo number_format( $c_seconds, 1 );
These two functions will round your number (will return 10.8 in your example), so, if you want to just truncate to the first decimal place (result to be equal to 10.7), you can use the following:
echo substr($c_seconds, 0, strpos($c_seconds, ".") + 2);
Actually, you can use the solutions from this question to better use number_format() and get your desired result.
echo sprintf('%0.1f', $c_seconds);
relevant docs here: http://php.net/sprintf
http://php.net/manual/en/function.number-format.php numberformat is propably what you are looking for.

Categories