php - Creation of new objects slows down? -
i searching why script slows down growing number of objects. not find error on side. script creating objects. i've disabled possible variables.
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
the first has high time because of include() in autoloader. __construct() has new sub objects don't raises fast original new operator. thought accessing object done handler in o(1) , not in o(n). i'm using php 5.2.3.
anyone helpful advice?
thank you.
Comments
Post a Comment