我在搜索为什么我的脚本会因为越来越多的对象而变慢。但是我找不到我的错误。该脚本只是创建对象。我已经禁用了所有可能的变量。
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
由于自动装带器中的include(),第一个时间很长。 __construct()有一些新的子对象,但不会像原始的new运算符一样快。我认为访问对象是由O(1)中的处理程序完成的,而不是在O(n)中。我正在使用PHP 5.2.3。
有任何有用建议的人吗?
谢谢。