Posts

Showing posts with the label abstraction layer

Members, __set, __get, ArrayAccess and Iterator

Lessons learned: __set() is 5 times slower than setting a member __get() + __set() is 13 times slower than incrementing a member Iterator is 4 times slower than using a member ArrayAccess is 3 times slower than setting an element in a member array ArrayAccess is 6 times slower than incrementing an element in a member array Here is the code: class test1 { public $test = null; private $_test = null; public function __set($id, $val) { $this->_test = $val; } } $start = microtime(true); $c = new test1(); for ($i=0; $i<1000000; $i++) $c->test = $i; echo number_format(microtime(true)-$start, 4)."\n"; // 0.1830s $start = microtime(true); $c = new test1(); for ($i=0; $i<1000000; $i++) $c->test2 = $i; echo number_format(microtime(true)-$start, 4)."\n"; // 0.9570s class test2 { private $_data = ['test4'=>0]; public $test3 = 0; public function __set($id, $val) { $this->_data[$id] = $val; } public function __get($id) { ...

MySQL or MySQLi or PDO

Lessons learned: MySQLi is 3-4 times slower than MySQL when fetching less then 500 datasets MySQLi is 2-4 times faster than MySQL when fetching more than 500 datasets PDO is 2-5 times slower than MySQL/MySQLi Unbuffered queries are 15-40 percent faster than buffered queries in MySQLi Unbuffered queries are 10-25 percent faster than buffered queries in MySQL for less than 10000 datasets Unbuffered queries are 3-7 percent slower than buffered queries in MySQL for more than 10000 datasets Unbuffered queries are 0-5 percent faster than buffered queries in PDO Non thread safe versions of PHP on win32 are 50 percent faster than thread safe versions Here is the test script: $table = 'test1.test2'; benchmark($table, 100); benchmark($table, 500); benchmark($table, 1000); benchmark($table, 5000); benchmark($table, 10000); benchmark($table, 50000); benchmark($table, 100000); function benchmark($table, $size) { mysql_connect('127.0.0.1', 'root', ''); mysql_qu...

Disadvantages of ORM

ORM has attracted a lot of attention in the last years. So let's get a bit deeper into it. The biggest advantage of ORM is also the biggest disadvantage: queries are generated automatically queries can't be optimized queries select more data than needed, things get slower, more latency (some ORMs fetch all datasets of all relations of an object even though only 1 attribute is read) compiling queries from ORM code is slow (ORM compiler written in PHP) SQL is more powerful than ORM query languages database abstraction forbids vendor specific optimizations Other problems coming up with ORM compiling ORM logic from phpDoc instructions or XML files is slow, but can be cached ORM validates relations and field names outside the database, but can't keep relations consistent ORM libraries are often used in projects without making a benchmark before ORM libraries are often used because the documentation of the library says it is very fast ORM libraries are often used by default with...