GIDForums

Go Back   GIDForums > Computer Programming Forums > MySQL / PHP Forum > PHP Code Library
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read


 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 11-Apr-2003, 05:54
JdS's Avatar
JdS JdS is offline
Senior Member
 
Join Date: Aug 2001
Location: KUL, Malaysia
Posts: 3,371
JdS will become famous soon enough

[class] Timer


PHP Code:

<?php

// NAME: Timer()
// VERSION: 1.0
// AUTHOR: J de Silva
// WEBSITE: [url]http://www.desilva.biz/[/url]
// DESCRIPTION: A simple PHP SCRIPT TIMER / BENCHMARK class.
//              See notes below.
// TYPE: class
// --------------------

// Some constants
if( !defined( 'NL' ) ):
  // newlines
  define( 'NL', "\r\n" );
endif;

class Timer
{
  var $st; // START time
    
  function Timer()
  {
    // set the START time
    $this->st = $this->pGetTime();
  }

  /**
    * Displays the timer on a web page; you may specify the HTML
    * by including it as a parameter.
    *
    * e.g.
    * $t->DisplayTimer( '</p>Page created in [DEC=5]</p>' );
    *
    * You may place this repeatedly at many points in your script,
    * (in the event you want to time some parts off your script incrementally).
    *
    * The [DEC=5] bit is where the results are displayed in the
    * given HTML string, meaning 5 decimal places! If you just
    * want to show 4 decimal places in the result, change the
    * parameter string to include [DEC=4], etc.
  */
  function DisplayTimer( $html='' )
  {
    $format = '<p style="text-align:center">Script runtime: %.5f %s</p>';
    if( !empty($html) ):
      $format = preg_replace( '/\[dec=([0-9]+)\]/i', '%.$1f %s', $html, 1 );    
    endif;
    $time = $this->pCalcTime();
    $sec = ( ($time > 1) ? 'secs.' : 'sec.' );
    printf( $format.NL, $time, $sec );
  }

  /**
    * This is the same as above, except only useful if you need
    * the results / variable to add into a template (i.e. minus
    * any html).
    *
    * e.g.
    * $variable = $t->ReturnTimer( 5 );
    *
    * The parameter is the number of decimal places in
    * the returned value
    * 
    * See sample codes in next post.
    */    
  function ReturnTimer( $decimal=5 )
  {
    if( !is_int($decimal) ):
      $decimal = 5;
    endif;
    $format = '%.'.$decimal.'f %s';
    $time = $this->pCalcTime();
    $sec = ( ($time > 1) ? 'secs.' : 'sec.' );
    return sprintf( $format, $time, $sec );
  }
    
  function pCalcTime()
  {
    return ( $this->pGetTime() - $this->st );
  }

  function pGetTime()
  {
    $mt = explode( ' ', microtime() );    
    return $mt[1] + $mt[0];
  }
}

?>


please also see: PHP Timer function
  #2  
Old 11-Apr-2003, 06:32
JdS's Avatar
JdS JdS is offline
Senior Member
 
Join Date: Aug 2001
Location: KUL, Malaysia
Posts: 3,371
JdS will become famous soon enough

Sample code to test TIMER class


Let's say you save the TIMER class above as timer.lib.php.

In the same folder, you have a PHP script saved as test.php and inside this file you add the following code:

PHP Code:

<?php

// include the class file
include_once( 'timer.lib.php' );

// Start the TIMER
$overall = new Timer();

// Some constants
if( !defined( 'NL' ) ):
  // newlines
  define( 'NL', "\r\n" );
endif;


// Some dummy code
echo '<h1>Some dummy code</h1>'.NL;

for( $i=1; $i<6; $i++ ):

  // Start individual instances of the timer for each loop
  $t{$i} = new Timer();

  echo '<h2>Loop No.'.$i.'</h2>'.NL; 
  for( $n=0; $n<25; $n++ ):
    echo '<p>The quick brown fox jumped over the lazy dogs.</p>'.NL;
  endfor;

  // Display the timer but timing each LOOP
  $t{$i}->DisplayTimer( '<p style="text-align:center">This loop took [DEC=6]</p>' ); 

endfor;

// Let's display how long the entire script took to generate
// the WHOLE page.
$overall->DisplayTimer();

// I think that's about right... :)

?>


Let me know if this doesn't work for you, cause I am coding blind here (IOW, I didn't test it out yet).
  #3  
Old 29-Apr-2003, 12:20
Elmseeker's Avatar
Elmseeker Elmseeker is offline
Awaiting Email Confirmation
 
Join Date: Jan 2003
Posts: 87
Elmseeker is on a distinguished road
Personally, I would have made this one a function call instead of a class but it works nicely this way too. :)
 

Recent GIDBlogNew Corolla Altis, 10th Generation - Part I by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
[class] 404 search function code jrobbio MySQL / PHP Forum 6 22-Apr-2003 09:32
[function] Timer JdS PHP Code Library 0 18-Jan-2003 18:11
[class] Auto add / strip slashes regardless of setting. JdS PHP Code Library 0 17-Jan-2003 01:35

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 12:50.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.