Page 1 of 1
Crop string output?
Posted: Sat Mar 22, 2008 1:56 am
by JAB Creations
How can I crop the seconds of the following script so...
Rendered in 0.000921964645386 seconds.
...would be rendered as...
Rendered in 0.00092 seconds.
?
Here is the script...
Code: Select all
<?php
$time_start = microtime(true);
// Sleep for a while
usleep(0);
$time_end = microtime(true);
$time = $time_end - $time_start;
echo "Rendered in $time seconds.\n";
?>
Re: Crop string output?
Posted: Sat Mar 22, 2008 2:09 am
by John Cartwright
number_format(), substr(), sprintf(), etc, etc
Re: Crop string output?
Posted: Sat Mar 22, 2008 2:21 am
by JAB Creations
Thanks, I have used substr before though I couldn't remember off-hand.
Code: Select all
<?php
$time_start = microtime(true);
// Sleep for a while
usleep(0);
$time_end = microtime(true);
$time = $time_end - $time_start;
//echo "Rendered in $time seconds\n";
echo 'Rendered in ';
echo substr($time, 0, 7);
echo ' seconds.';
?>
Re: Crop string output?
Posted: Sat Mar 22, 2008 3:12 am
by Chris Corbyn
round()?
Re: Crop string output?
Posted: Sat Mar 22, 2008 3:18 am
by JAB Creations
Thanks Chris, the suggestion was closer in context though I'm happy to know about both.
Code: Select all
<?php
$time_start = microtime(true);
// Sleep for a while
usleep(0);
$time_end = microtime(true);
$time = $time_end - $time_start;
//echo "Rendered in $time seconds\n";
echo 'Rendered in ';
//echo substr($time, 0, 7);
echo round($time, -7);
echo ' seconds.';
?>
Re: Crop string output?
Posted: Sat Mar 22, 2008 7:39 pm
by s.dot
You will want the second parameter of round() to be positive. Suppose your script output 0.0090 exactly.
round($time, 5) should do!
Re: Crop string output?
Posted: Sat Mar 22, 2008 7:46 pm
by JAB Creations
I tried that though locally with PHP 5.2.0 I get...
Rendered in 2.19E-005 seconds.
Uh...?

Re: Crop string output?
Posted: Sat Mar 22, 2008 9:36 pm
by s.dot
exponentially fast ;d
same as .0000219
Re: Crop string output?
Posted: Sat Mar 22, 2008 9:43 pm
by JAB Creations
I'm sorry you lost me?
Re: Crop string output?
Posted: Sun Mar 23, 2008 12:06 am
by John Cartwright
JAB Creations wrote:I'm sorry you lost me?
It was scottay's attempt at "humor"
FYI, 2.19E-005 === .0000219, which is what he was getting at
Re: Crop string output?
Posted: Sun Mar 23, 2008 12:08 am
by JAB Creations
Oh ok, I'm not afraid to admit lots of people know lots of stuff that I don't.
So is the value technically correct though just not displaying humanly readable or?
Re: Crop string output?
Posted: Sun Mar 23, 2008 2:41 am
by s.dot
Yes, it is correct. I seriously doubt you'll have any pages that load fast enough to get the E in your time output. of course usleep(0) would do that.. if that's all you have on your page.
do usleep(1000);

Re: Crop string output?
Posted: Sun Mar 23, 2008 5:07 am
by JAB Creations
So many choices!
Though I want the most accurate/honest/context-correct method I can/should use. Here are some err 'benchmarks' I ran locally...
echo substr($time, 0, 7);
0.00046
0.00034
0.00036
echo round($time, 7) && usleep(1);
0.0008402
0.0005021
0.0006831
0.0007231
-1.9E-006
echo round($time, 7) && usleep(1000);
-1.9E-006
0.0156231
0.0573151
0.0014811
0.001811
0.0010831
0.0572259
When I see E does that mean my website is faster then the Enterprise?