Page creation time

Small, short code snippets that other people may find useful. Do you have a good regex that you would like to share? Share it! Even better, the code can be commented on, and improved.

Moderator: General Moderators

Post Reply
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Page creation time

Post by JayBird »

i do it like this

Code: Select all

//put at begining of file

function utime (){
$time = explode( " ", microtime());
$usec = (double)$time[0];
$sec = (double)$time[1];
return $sec + $usec;
}
$start = utime();

Code: Select all

//put at end of page before /body and /h // tml tags
$end = utime();
$run = $end - $start;
echo "Page created in " . substr($run, 0, 5) . " secs.";
Mark
User avatar
Stoker
Forum Regular
Posts: 782
Joined: Thu Jan 23, 2003 9:45 pm
Location: SWNY
Contact:

Post by Stoker »

Thatone is wasting quite a bit of resources and time to meassure the time, something like this is more efficient..

Code: Select all

<?php

 $starttime = (float) array_sum( explode(' ', microtime()));

 // ---  Stuff that takes time ---
 

 $i = 500000;
 while ($i) $i--;


 // --- end of stuff that takes time

 $executiontime = (float) array_sum( explode(' ', microtime())) - $starttime;

 // log or store or echo or whatever it is useful for..
 echo 'Page created in ' . sprintf('%.3f',$executiontime) . ' secs.'."\n";


?>
Edit/Add: Using a class as posted later is even more waste of resources, if you want to meassure how long your page takes to load I assume you are converned with resource usage and code profiling, don't make it worse by adding extra cr"p that just slows down your profiling work..
Last edited by Stoker on Tue Mar 30, 2004 9:41 pm, edited 1 time in total.
Goowe
Forum Commoner
Posts: 94
Joined: Mon Mar 15, 2004 9:51 am
Location: Southeast Alaska

Post by Goowe »

8O I've always wondered how it was done! I'm glad to see them!
k7k0
Forum Newbie
Posts: 2
Joined: Tue Mar 30, 2004 2:40 pm

Post by k7k0 »

Sorry for my english :D
Here is a class encapsulating the script.

Code: Select all

/*
*   Compute the time transcurred between two moments
*/
class Chronometer
{
    var $startTime;
    var $endTime;

    function start()
    {
        $this->startTime=gettimeofday();   
    }
    
    function stop()
    {
        $this->endTime=gettimeofday();
    }

    function show($precision=2)
    {
        $time = (float)($this->endTime['sec'] - $this->startTime['sec']) + ((float)($this->endTime['usec'] - $this->startTime['usec'])/1000000);
        return (round($time, $precision));
    }
}
Then use:

Code: Select all

$chrono = new Chronometer();
$chrono->start();
/*
* Here go some logic
*/
$chrono->stop();

echo $chrono->show();
Post Reply