Code Snippet
Count Script Excecution Time
$execution_time = microtime(); # Start counting
# Your code
$execution_time = microtime() - $execution_time;
$execution_time = sprintf('It took %.5f sec', $execution_time);
$execution_time = microtime(); # Start counting
# Your code
$execution_time = microtime() - $execution_time;
$execution_time = sprintf('It took %.5f sec', $execution_time);
$execution_time = sprintf(‘It took %.5f sec’, $execution_time);
echo $execution_time;
# or
printf(‘It took %.5f sec’, $execution_time);
# to display the output.
This technique will not work correctly!
I would suggest something like this…
$time = microtime();
$time = explode(' ', $time);
$time = $time[1] + $time[0];
$start = $time;
// Your code.
$time = microtime();
$time = explode(' ', $time);
$time = $time[1] + $time[0];
$finish = $time;
$page_time = ($finish - $start);
$page_time = round($page_time, 3); // For users.
echo $page_time;
/* By daGrevis. */
Here’s the stripped down version of daGrevis’ fine work:
$startTime = array_sum(explode(' ', microtime()));
/* Your code here */
$totalTime = array_sum(explode(' ', microtime())) - $startTime;
echo $totalTime;
Since php 5.1, the timestamp of the start of the request is available in $_SERVER['REQUEST_TIME']
So you can do :
$page_time = round(microtime(true)-$_SERVER['REQUEST_TIME'], 3);
Perhaps “register_shutdown_function()” could be of some use here :)
I time all my programs and log the timing to a file to help me tune my database system. I have developed a class to help.
in my programs, after I make sure the class is loaded (I depend on __autoload) All I need is one line.
As part of php cleanup, the destructor will run and print out the timing. I have the instantiation statement in a require_once file.
In the example above it just prints the result. My actual destructor writes to a log file so I can average timings.,
DigWP
A book and blog co-authored by Jeff Starr and myself about the World's most popular publishing platform.
Quotes on Design
Design, like Art, can be an elusive word to define and an awfully fun thing to have opinions about.
HTML-Ipsum
One-click copy to clipboard access to Lorem Ipsum text that comes wrapped in a variety of HTML.
Bookshelf
Hey Chris, what books do you recommend? These, young fertile mind, these.