PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
JAB Creations
DevNet Resident
Posts: 2341 Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:
Post
by JAB Creations » Sat Mar 22, 2008 1:56 am
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";
?>
John Cartwright
Site Admin
Posts: 11470 Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:
Post
by John Cartwright » Sat Mar 22, 2008 2:09 am
number_format(), substr(), sprintf(), etc, etc
JAB Creations
DevNet Resident
Posts: 2341 Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:
Post
by JAB Creations » Sat Mar 22, 2008 2:21 am
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.';
?>
JAB Creations
DevNet Resident
Posts: 2341 Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:
Post
by JAB Creations » Sat Mar 22, 2008 3:18 am
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.';
?>
s.dot
Tranquility In Moderation
Posts: 5001 Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana
Post
by s.dot » Sat Mar 22, 2008 7:39 pm
You will want the second parameter of round() to be positive. Suppose your script output 0.0090 exactly.
round($time, 5) should do!
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
JAB Creations
DevNet Resident
Posts: 2341 Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:
Post
by JAB Creations » Sat Mar 22, 2008 7:46 pm
I tried that though locally with PHP 5.2.0 I get...
Rendered in 2.19E-005 seconds.
Uh...?
s.dot
Tranquility In Moderation
Posts: 5001 Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana
Post
by s.dot » Sat Mar 22, 2008 9:36 pm
exponentially fast ;d
same as .0000219
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
JAB Creations
DevNet Resident
Posts: 2341 Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:
Post
by JAB Creations » Sat Mar 22, 2008 9:43 pm
I'm sorry you lost me?
John Cartwright
Site Admin
Posts: 11470 Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:
Post
by John Cartwright » Sun Mar 23, 2008 12:06 am
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
JAB Creations
DevNet Resident
Posts: 2341 Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:
Post
by JAB Creations » Sun Mar 23, 2008 12:08 am
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?
s.dot
Tranquility In Moderation
Posts: 5001 Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana
Post
by s.dot » Sun Mar 23, 2008 2:41 am
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);
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
JAB Creations
DevNet Resident
Posts: 2341 Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:
Post
by JAB Creations » Sun Mar 23, 2008 5:07 am
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?