0

我正在寻找为什么我的脚本会随着对象数量的增加而变慢。但我找不到我这边的错误。该脚本只是创建对象。我已经禁用了所有可能的变量。

for ($i = 0; $i < $ilen; $i++)
{
    $row = db_row($res, $i);
    echo str_replace(" ", "&nbsp;", 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) . "&micro;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  

由于自动加载器中的 include(),第一个时间很长。__construct() 有一些新的子对象,但不会像原来的 new 运算符那样快速提升。我认为访问对象是由 O(1) 中的处理程序完成的,而不是 O(n) 中的。我正在使用 PHP 5.2.3。

有人有有用的建议吗?

谢谢你。

4

0 回答 0