Код: Выделить всё
runtimeВ некоторых случаях быстрее клонировать предыдущий объект, чем пересчитывать все заново (например, когда одни и те же входные параметры дают тот же результат).
Однако при клонировании объекта
Код: Выделить всё
runtimeВот упрощенный пример:
Код: Выделить всё
class Object
{
protected $runtime;
public function getRuntime()
{
return $this->runtime;
}
public function doSome(/*...*/)
{
$start = microtime(true);
// ... the heavy work ...
// ...
$this->runtime = microtime(true) - $start;
}
}
$objects = [];
while (/*...*/) {
if (count($objects) > 0) {
$start = microtime(true);
if (/*check if would get the same result as the previous one*/) {
$object = clone end($objects);
// MUST change the runtime here on the clone
// but i should not make :runtime public
$object->runtime = microtime(true) - $start; // :(
$objects[] = $object;
continue;
}
}
$object = new Object();
$object->doSome(/*...*/);
$objects[] = $object;
}
Подробнее здесь: https://stackoverflow.com/questions/490 ... -the-clone
Мобильная версия