<?php
class Benchmark
{
private static $max, $memory;
public static function memoryTick()
{
self::$memory = memory_get_usage() - self::$memory;
self::$max = self::$memory>self::$max?self::$memory:self::$max;
self::$memory = memory_get_usage();
debug_print_backtrace();
}
public static function benchmarkMemory(callable $function, $args=null)
{
declare(ticks=1);
self::$memory = memory_get_usage();
self::$max = 0;
register_tick_function('call_user_func_array', ['Benchmark', 'memoryTick'], []);
$result = is_array($args)?
call_user_func_array($function, $args):
call_user_func_array($function);
unregister_tick_function('call_user_func_array');
return [
'memory' => self::$max
];
}
}
var_dump(Benchmark::benchmarkMemory('str_repeat', ['test',1E4]));
var_dump(Benchmark::benchmarkMemory('str_repeat', ['test',1E3]));
preferences:
31.55 ms | 402 KiB | 5 Q