📜 ⬆️ ⬇️

Optimize model usage from Active Record, and some arrays

I recently did a test that gave curious data. With whom I want to share, in order to help many clarify controversial situations.
Everywhere they just write that how fast the arrays are, how slow Active Record is ... But when you see specific numbers it is much easier to understand what is better than another.

Materials used:


- regular laptop: Intel core 2 duo 2.13GHz, RAM 6 GB
- php 5.3
- apache 2.2
- framework Yii 1.1.10
- Active Record


')

Test description:


There is a class -
class Address extends CActiveRecord { … } 

There is a cycle of a million (1,000,000) iterations. At each iteration:
  1. A new object is declared and filled with data.
     $address = new Address(); 

  2. The singlton pattern is used - the object is declared once and at each iteration and refilled.
     $address = Address::model(); 

  3. The array is declared and populated.
     $address = array(); 

  4. Simple stdClass ().
     $address = new stdClass(); 

  5. Declare the object (as in option 1) but without initializing all the associated AR data
     $address = new Address(null); 



Results (sec.):


  1. 28.490615844727
  2. 7.2354989051819
  3. 4.5744869709015
  4. 5.9930000305176
  5. 9.5185680294037


Findings:




I want to know why the class inherited from ActiveRecord spends so much time. As a result of tests, we see that most of the time is spent on initialization / assignment of properties / elements, i.e. the more you have to use properties / elements, the longer the object / array will work. And since ActiveRecord must describe in itself all the table fields (for this, there are CActiveRecordMetaData and CmysqlTableSchema and CmysqlColumnSchema classes that have their dofig properties), then this description of the meta data and the main execution time of the class inherited from ActiveRecord are spent.
From this you can make

More Conclusions:




Sample test code:



 public function actionIndex() { $mk = microtime(true); for ($i = 0; $i < 1000000; $i++) { $this->test1(); } echo microtime(true) - $mk, '< br/ >'; … } public function test1(){ $address = new Address(); $address ->zip = 3423423; $address ->state_ id = 23332; $address ->house = 2234; $address ->street = 'asdfasdf'; $address ->street_type = 'asdfasdfasdfsdf'; $address ->address = 'asdfasdfsdf'; $address ->code = 's'; $address ->name = 'asdfasdfasf'; $address ->latitude = 23.23232; $address ->longitude = 23.342342; } 


The rest is by analogy.

PS


  1. I note that this test only affects php and in no way depends on the optimizations of mysql or apache.
  2. It should be borne in mind that the time data of the tests is not the ideal time for an array or an object to work under ideal conditions, but they allow you to compare the speed of work relative to each other.


Pps


The class Address () is invented and properties are selected approximately according to its meaning.

Source: https://habr.com/ru/post/146707/


All Articles