<?php

namespace Core;

/**
 * Benchmark
 */
class Benchmark
{
	/**
	 * Format memory size string
	 *
	 * @param function $callback User function to profile
	 * @param int $count Iternations to make
	 * @example echo \Core\Benchmark::profile('myFunction');
	 * @return string Time taken in seconds (with precision of 3)
	 */
	static function profile($callback, $count = 1)
	{
		$n = 0;
		$t = microtime(true);
		while (1)
		{
			call_user_func($callback);
			if (++$n >= $count)
			{
				break;
			}
		}
		return round(0.0005 + microtime(true) - $t, 3);
	}
	
	private static $start;
	
	private static $stop;

	/**
	 * Save current time for measurement
	 *
	 * @return float Start time in seconds
	 * @example \Core\Benchmark::start();
	 */
	static function start()
	{
		return self::$start = round(0.0005 + microtime(true), 3);
	}
	
	/**
	 * Return time taken since last start
	 *
	 * @return float Time taken in seconds
	 * @example die(\Core\Clock::getTimeText(\Core\Benchmark::stop()));
	 */
	static function stop()
	{
		return self::$stop = round(0.0005 + microtime(true) - self::$start, 3);
	}
}
