1 <?php
defined('SYSPATH') OR die('No direct access allowed.');
5 * $Id: Benchmark.php 3917 2009-01-21 03:06:22Z zombor $
9 * @copyright (c) 2007 Kohana Team
10 * @license http://kohanaphp.com/license.html
12 final class Benchmark
{
14 // Benchmark timestamps
15 private static $marks;
18 * Set a benchmark start point.
20 * @param string benchmark name
23 public static function start($name)
25 if ( ! isset(self
::$marks[$name]))
27 self
::$marks[$name] = array
29 'start' => microtime(TRUE),
31 'memory_start' => function_exists('memory_get_usage') ?
memory_get_usage() : 0,
32 'memory_stop' => FALSE
38 * Set a benchmark stop point.
40 * @param string benchmark name
43 public static function stop($name)
45 if (isset(self
::$marks[$name]) AND self
::$marks[$name]['stop'] === FALSE)
47 self
::$marks[$name]['stop'] = microtime(TRUE);
48 self
::$marks[$name]['memory_stop'] = function_exists('memory_get_usage') ?
memory_get_usage() : 0;
53 * Get the elapsed time between a start and stop.
55 * @param string benchmark name, TRUE for all
56 * @param integer number of decimal places to count to
59 public static function get($name, $decimals = 4)
64 $names = array_keys(self
::$marks);
66 foreach ($names as $name)
68 // Get each mark recursively
69 $times[$name] = self
::get($name, $decimals);
76 if ( ! isset(self
::$marks[$name]))
79 if (self
::$marks[$name]['stop'] === FALSE)
81 // Stop the benchmark to prevent mis-matched results
85 // Return a string version of the time between the start and stop points
86 // Properly reading a float requires using number_format or sprintf
89 'time' => number_format(self
::$marks[$name]['stop'] - self
::$marks[$name]['start'], $decimals),
90 'memory' => (self
::$marks[$name]['memory_stop'] - self
::$marks[$name]['memory_start'])