3v4l.org

run code in 300+ PHP versions simultaneously
<?php error_reporting(-1); ini_set('display_errors', '1'); ini_set('display_startup_errors', '1'); mb_internal_encoding('UTF-8'); mb_http_output('UTF-8'); gc_enable(); class Ubench { protected $start_time; protected $end_time; protected $memory_usage; /** * Sets start microtime * * @return void */ public function start() { $this->start_time = microtime(true); } /** * Sets end microtime * * @return void */ public function end() { $this->end_time = microtime(true); $this->memory_usage = memory_get_usage(true); } /** * Returns the elapsed time, readable or not * * @param boolean $readable Whether the result must be human readable * @param string $format The format to display (printf format) * @return string|float */ public function getTime($raw = false, $format = null) { $elapsed = $this->end_time - $this->start_time; return $raw ? $elapsed : self::readableElapsedTime($elapsed, $format); } /** * Returns the memory usage at the end checkpoint * * @param boolean $readable Whether the result must be human readable * @param string $format The format to display (printf format) * @return string|float */ public function getMemoryUsage($raw = false, $format = null) { return $raw ? $this->memory_usage : self::readableSize($this->memory_usage, $format); } /** * Returns the memory peak, readable or not * * @param boolean $readable Whether the result must be human readable * @param string $format The format to display (printf format) * @return string|float */ public function getMemoryPeak($raw = false, $format = null) { $memory = memory_get_peak_usage(true); return $raw ? $memory : self::readableSize($memory, $format); } /** * Wraps a callable with start() and end() calls * * Additional arguments passed to this method will be passed to * the callable. * * @param callable $callable * @return mixed */ public function run(callable $callable) { $arguments = func_get_args(); array_shift($arguments); $this->start(); $result = call_user_func_array($callable, $arguments); $this->end(); return $result; } /** * Returns a human readable memory size * * @param int $size * @param string $format The format to display (printf format) * @param int $round * @return string */ public static function readableSize($size, $format = null, $round = 3) { $mod = 1024; if (is_null($format)) { $format = '%.2f%s'; } $units = explode(' ','B Kb Mb Gb Tb'); for ($i = 0; $size > $mod; $i++) { $size /= $mod; } if (0 === $i) { $format = preg_replace('/(%.[\d]+f)/', '%d', $format); } return sprintf($format, round($size, $round), $units[$i]); } /** * Returns a human readable elapsed time * * @param float $microtime * @param string $format The format to display (printf format) * @return string */ public static function readableElapsedTime($microtime, $format = null, $round = 3) { if (is_null($format)) { $format = '%.3f%s'; } if ($microtime >= 1) { $unit = 's'; $time = round($microtime, $round); } else { $unit = 'ms'; $time = round($microtime*1000); $format = preg_replace('/(%.[\d]+f)/', '%d', $format); } return sprintf($format, $time, $unit); } } $bench = new Ubench; $testSum = 0; $bench->start(); // Execute some code for ($i = 0; $i < 1000000; $i++) { $j= $i; } $bench->end(); // Get elapsed time and memory echo "[" . $bench->getTime(); echo " | " . $bench->getTime(true); // elapsed microtime in float echo " | " . $bench->getTime(false, '%d%s') . "]"; echo "\r\n[" . $bench->getMemoryPeak(); echo " | " . $bench->getMemoryPeak(true); // memory peak in bytes echo " | " . $bench->getMemoryPeak(false, '%.3f%s') . "]"; // Returns the memory usage at the end mark echo "\r\n---\r\n" . $bench->getMemoryUsage(); exit(); ?>

preferences:
32.47 ms | 417 KiB | 5 Q