Extending PDO a waste…

I just spent some time writing a test script to compare using PDO by extending it, and by proxying it using call().

Let me start out by saying, when I first wrote Crtx_DB, I extended PDO, this meant you could easily fall back to native PDO calls if the Crtx_DB_DataObject CRUD automaticn wasn’t working for you, without needing to instantiate another object.

Then it was decided that PDO::construct() would be made final because if a user extended PDO and overwrote construct() without calling parent::construct() it caused PHP to segfault (or otherwise do bad things).

A couple of days ago, Ilia announced that the ability to extend PDO and overwrite PDO::construct() has now been restored.

Now, the nice thing I had done with Crtx_DB, was allow you to pass in any PDO/Crtx_DB object instead of a DSN if you wanted to use the same connection, thus not having to reconnect when using the same DB directly from PDO (why?!) or elsewhere with Crtx_DB.

However, I wasn’t sure if using call() to proxy an internally stored PDO object was faster than extending PDO – in fact, I was pretty sure it wasn’t.

However, after some benchmarking, it appears that I was (and happily so) wrong.

Extending PDO Vs using call() to proxy it, is approximately 5.5% slower on average, what this means is, using call() 100 times is slower than creating 100 new database connections.

So, if you have 100 Crtx_DB_DataObject objects (possibly in a big application with lots of tables, say fudForum) its cheaper (resource wise) to use the same DB connection 100 times and perform one query on each table, than it is to create a connection every time.

I wonder if anybody else thinking about extending PDO has done this math?

----------------------------------------------------------
marker        time index            ex time         perct
----------------------------------------------------------
Start         1127389417.54605300   -                0.00%
----------------------------------------------------------
Start Proxy   1127389417.54613800   0.000085         0.55%
----------------------------------------------------------
Stop          1127389417.56148300   0.015345        99.45%
----------------------------------------------------------
total         -                     0.015430       100.00%
----------------------------------------------------------
----------------------------------------------------------
marker        time index            ex time         perct
----------------------------------------------------------
Start         1127389417.56151900   -                0.00%
----------------------------------------------------------
Extend        1127389417.56156000   0.000041         0.02%
----------------------------------------------------------
Stop Extend   1127389417.83469900   0.273139        99.97%
----------------------------------------------------------
Stop          1127389417.83473700   0.000038         0.01%
----------------------------------------------------------
total         -                     0.273218       100.00%
----------------------------------------------------------

Difference: 5.6475049228089%

Update: Full benchmark script can be found here

– Davey

P.S.
I’m pretty sure my math is wrong, but you can see that the extend is slower – feel free to correct the math