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:
- A new object is declared and filled with data.
$address = new Address();
- The singlton pattern is used - the object is declared once and at each iteration and refilled.
$address = Address::model();
- The array is declared and populated.
$address = array();
- Simple stdClass ().
$address = new stdClass();
- Declare the object (as in option 1) but without initializing all the associated AR data
$address = new Address(null);
Results (sec.):
- 28.490615844727
- 7.2354989051819
- 4.5744869709015
- 5.9930000305176
- 9.5185680294037
Findings:
- Of course, you can use objects for storing and processing data, not just arrays.
- Of course, classes can be inherited from CActiveRecord , even if you are not going to use the classic Active Record approach - for this we do not forget to add null to the constructor (pay attention - not
false , not empty string , not 0 ):
$address = new Address(null);
and avoid recording:
$address->attributes = $_POST['Address']
because at the same time, the metadata AR is still connected and the object operation time increases.
Making an assignment in manual:
$address->street = $_POST['Address']['street'];
- Of course you need to use the method - model () - Caution! This applies to Yii <v.2
- It is not necessary to rest only on arrays - they should also be used wisely. For convenience, they do not go to any comparison with the objects.
- The most important conclusion is that one must always (turn on the brain) find the “middle ground” between the speed of product development and the speed of product operation. For example, if there are no at least 50,000 visits per day and there are no lists of some data from the database on the site, then you can forget about arrays and not remember until a bottleneck appears and there will be no brakes.
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:
- The more columns in the table ActiveRecord will raise, the longer our class will be initialized.
- Please note that this does not depend on how many fields you put in the SELECT - meta data will in any case go up (if I am not mistaken, of course). And against that, the speed of DAO operation will directly depend on the number of fields in the SELECT.
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
- I note that this test only affects php and in no way depends on the optimizations of mysql or apache.
- 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.