Skip to content

Instantly share code, notes, and snippets.

@driscollwebdev
Created March 31, 2011 20:27
Show Gist options
  • Save driscollwebdev/897191 to your computer and use it in GitHub Desktop.
Save driscollwebdev/897191 to your computer and use it in GitHub Desktop.
The ScriptTimer class...
<?php
/**
*
* The ScriptTimer class provides an easy and efficient method for identifying
* time sinks in your code. Ideally you'd insert time markers around lines of
* code that you believe are causing your application to run slowly so that you
* can identify the slow-running culprit and fix it!
*
* @author Brian Driscoll
* @license The MIT License
Copyright (c) 2011 Brian Driscoll
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
class ScriptTimer {
private $markers;
public function __construct(){
$this->markers = array();
}
/**
* The mark method creates a time marker and stores it, along with an optional
* message, in the instance's markers array.
* @param string $message
*/
public function mark($message=NULL){
$marktime = microtime(true);
$timestep = 0;
$elapsed = 0;
$message = !is_null($message) ? $message : "(No message.)";
$isStart = count($this->markers)==0;
if(!$isStart){
$timestep = $marktime - $this->markers[count($this->markers)-1]["marktime"];
$elapsed = $marktime - $this->markers[0]["marktime"];
}
$mark = array();
$mark["marktime"] = $marktime;
$mark["timestep"] = $timestep;
$mark["elapsed"] = $elapsed;
$mark["message"] = $message;
$this->markers[] = $mark;
}
/**
* The toLog method creates a tab-delimited string containing all time markers
* stored with this instance of the ScriptTimer
* @return string
*/
public function toLog(){
$logOutput = "marktime\ttimestep\telapsed\tmessage\r\n";
foreach($this->markers as $marker){
$logOutput .= $this->createLogEntry($marker);
}
return $logOutput;
}
/**
* The createLogEntry method is called internally to create
* a tab-delimited row from a time marker that is passed as
* a parameter.
* @param array $mark
* @return string
*/
private function createLogEntry($mark){
$output = $mark["marktime"] . "\t";
$output .= $mark["timestep"] . "\t";
$output .= $mark["elapsed"] . "\t";
$output .= $mark["message"] . "\r\n";
return $output;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment