オブジェクトの数が増えるとスクリプトの速度が低下する理由を探していました。しかし、私は自分の側でエラーを見つけることができませんでした。スクリプトはオブジェクトを作成しているだけです。可能なすべての変数を無効にしました。
for ($i = 0; $i < $ilen; $i++)
{
$row = db_row($res, $i);
echo str_replace(" ", " ", str_pad("obj" . $i, 10));
$time = microtime(true);
$element = new dataStructureElement($row['code']);
echo "New: " . number_format((microtime(true) - $time) * 1000, 1) . "ms\n";
}
class dataStructureElement {
...
function __construct($code, $recover = false)
{
$time = microtime(true);
parent::__construct();
$this->code = $code = enc::seourl($code);
$this->key = 'element:' . $code;
if ($recover) $this->recover();
$this->properties = new dataStructureProperties($code);
$this->rules = new dataStructurePropertiesRules($code);
$this->searchIndex = new dataSearchIndexElement($code);
$this->searchGroup = new dataSearchGroupElement($code);
echo "__constuct(): " . number_format((microtime(true) - $time) * 1000000, 1) . "µs ";
}
...
}
obj0 __constuct(): 4,512.8µs New: 5.6ms
obj1 __constuct(): 150.9µs New: 1.5ms
obj2 __constuct(): 149.0µs New: 1.2ms
obj3 __constuct(): 141.1µs New: 1.1ms
obj4 __constuct(): 142.8µs New: 1.1ms
obj5 __constuct(): 140.9µs New: 1.1ms
obj6 __constuct(): 197.9µs New: 1.4ms
...
obj1993 __constuct(): 237.9µs New: 11.3ms
obj1994 __constuct(): 245.1µs New: 11.8ms
obj1995 __constuct(): 240.8µs New: 18.9ms
obj1996 __constuct(): 250.1µs New: 12.0ms
obj1997 __constuct(): 303.0µs New: 12.3ms
obj1998 __constuct(): 252.0µs New: 12.4ms
obj1999 __constuct(): 338.1µs New: 12.3ms
1 つ目は、オートローダーの include() のために時間がかかります。__construct() にはいくつかの新しいサブオブジェクトがありますが、元の new 演算子ほど速く発生しません。オブジェクトへのアクセスは、O(n) ではなく O(1) のハンドラーによって行われると思いました。PHP 5.2.3 を使用しています。
役に立つアドバイスをくれる人はいますか?
ありがとうございました。