一個帶有變數輸入的工作範例,用於驗證演算法的漸進分析
<?php
$n = 1000; // 輸入大小
declare(ticks=1);
class Counter {
private $counter = 0;
public function increase()
{
$this->counter++;
}
public function print()
{
return $this->counter;
}
}
$obj = new Counter;
register_tick_function([&$obj, 'increase'], true);
for ($i = 0; $i < 100; $i++)
{
$a = 3;
}
// unregister_tick_function([&$obj, 'increase']);
// 不確定如何註銷,您可以改用 Counter 中的靜態方法和成員。
var_dump("基本低階操作次數: " . $obj->print());
?>